Create UIImage from 2 UIImages and label

一笑奈何 提交于 2020-01-23 09:03:16

问题


I got one big UIImage. Over this UIImage i got one more, witch is actually a mask. And one more - i got UILabel over this mask! Witch is text for the picture.

I want to combine all this parts in one UIImage to save it to Camera Roll!

How should I do it?

UPD. How should i add UITextView?

i found:

[[myTextView layer] renderInContext:UIGraphicsGetCurrentContext()];

But this method doesn't place myTextView on the right place.


回答1:


create two UIImage objects and one UILabel objects then use drawInRect: method

//create image 1

UIImage *img1 = [UIImage imageNamed:@"image1.png"];

//create image 2    

UIImage *img2 = [UIImage imageNamed:@"image2.png"];

// create label

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50,50 )];

//set you label text

[label setText:@"Hello"];

// use UIGraphicsBeginImageContext() to draw them on top of each other

//start drawing
UIGraphicsBeginImageContext(img1.size);

//draw image1

[img1 drawInRect:CGRectMake(0, 0, img1.size.width, img1.size.height)];

//draw image2

[img2 drawInRect:CGRectMake((img1.size.width - img2.size.width) /2, (img1.size.height- img2.size.height)/2, img2.size.width, img2.size.height)];

//draw label

[label drawTextInRect:CGRectMake((img1.size.width - label.frame.size.width)/2, (img1.size.height - label.frame.size.height)/2, label.frame.size.width, label.frame.size.height)];

//get the final image

UIImage *resultImage  = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

The resultImage which is UIImage contains all of your images and labels as one image. After that you can save it where ever you want.

Hope helps...



来源:https://stackoverflow.com/questions/12677904/create-uiimage-from-2-uiimages-and-label

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!