Create UIImage from 2 UIImages and Shortcuts

I have one big UIImage . Over this UIImage I got another one, the witch is actually a mask. And one more - I got UILabel over this mask! A witch is the text for the image.

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

How can I do it?

UPD How to add a UITextView ?

I found:

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

But this method does not put myTextView in the right place.

+4
source share
1 answer

create two UIImage and one UILabel object, then use the drawInRect: method drawInRect:

 //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(); 

resultImage UIImage contains all your images and tags as one image. After that, you can save it wherever you want.

Hope helps ...

+10
source

Source: https://habr.com/ru/post/1437252/


All Articles