IPhonesdk Combine three images with two images

I am working on an iphone application where I need to combine three images and make them one image. I want to say that I have a background image, a header image and a lower image, I need to combine all this to make one image so that I can use it to post to facebook. thanks.

* EDIT * I know this code for two images, but how can I use it for three images:

UIGraphicsBeginImageContext(saveView.bounds.size); [saveView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); 
+6
source share
2 answers

You can just align them in one UIView (I think it’s even off screen, but I haven’t checked yet) - and then just convert this UIView to UIImage with QuartzCode:

 UIGraphicsBeginImageContext(myView.bounds.size); [myView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); 

Then turn this into a format - like PNG:

 NSData *imageData = UIImagePNGRepresentation(image); 

Then sending should not be too complicated.

EDIT Here is an extended example that you can also see for 3 images - you can, of course, use the interface builder and Outlets instead of writing everything, but you can copy it to try:

 UIImageView *imgView1, *imgView2, *imgView3; imgView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image1"]]; imgView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image2"]]; imgView3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image3"]]; imgView1.frame = CGRectMake(0, 0, 50, 50); imgView2.frame = CGRectMake(50, 50, 100, 100); imgView3.frame = CGRectMake(100, 100, 200, 200); [referenceView addSubview:imgView1]; [referenceView addSubview:imgView2]; [referenceView addSubview:imgView3]; UIGraphicsBeginImageContext(referenceView.bounds.size); [referenceView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); resultView = [[UIImageView alloc] initWithImage:finalImage]; resultView.frame = CGRectMake(0, 0, 320, 480); [self.view addSubview:resultView]; referenceView.hidden = YES; 

Note I checked and the UIView should be accessible for drawing / visibility during the call to renderInContext (it may be off-screen, but it cannot be hidden or alpha = 0, because then it will be displayed invisible). So either set it aside on the screen or hide it immediately after drawing

+10
source

Shein's answer helped me, but I found that I did not need to add a UIView to the main view in order to display it. Here is an example of adding text to an image ...

  UIImage *image = [UIImage imageNamed:@"toolbar_icon"]; // image is 20 x 20 .png UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)]; [tempView addSubview:[[UIImageView alloc] initWithImage:image]]; UILabel *label = [[UILabel alloc] initWithFrame:tempView.frame]; [label setText:[NSString stringWithFormat:@"%d", [self countValue]]]; [label setFont:[UIFont systemFontOfSize:22.0]]; [label setTextColor:[UIColor lightTextColor]]; [label setTextAlignment:UITextAlignmentCenter]; [label setBackgroundColor:[UIColor clearColor]]; [tempView addSubview:label]; UIGraphicsBeginImageContext(tempView.bounds.size); [tempView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); UIBarButtonItem *countItem = [[UIBarButtonItem alloc] initWithImage:finalImage style:UIBarButtonItemStyleBordered target:self action:@selector(countAction:)]; 

NOTE. I suddenly realized that my answer included adding a label to the image that I am trying to make before finding this. Link Merge Label Text with Image

+2
source

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


All Articles