wayneh
wayneh

Reputation: 4413

Saving two UIImages side-by-side as one combined image?

Xcode 5, iOS 7

Two UIViews side-by-side - viewA and viewB. Each view contains a UIImage - imageA and imageB. Both images meet at border between the views so that they appear seamless:imageAimageB.

How can I save the two images into a single image file, side-by-side, as though they were one image?

I know I could take a screenshot, but that would lower the resolution, and would not account for portions of the images which may be off-screen (due to scaling or positioning).

This may answer my own question but the best I can think of to create a new UIImage (imageC), size it to account for imageA and imageB, then copy the images into imageC based on their relative positions.

Any easier way?

Upvotes: 1

Views: 1855

Answers (2)

BlackSheep
BlackSheep

Reputation: 1087

using 2 UIImageView in you interface, after using the UIImagePicker for each, you can marge with this code:

    - (IBAction)margeSave:(id)sender{

        //here you get you two different image

        UIImage *bottomImage = self.imageViewPick.image;
        UIImage *image       = self.myImage.image;

        //here  you have to crop each image with the code below
        //using here a crop code and adjust for your Image

        // create a new size for a merged image

        CGSize newSize = CGSizeMake(640, 640);
        UIGraphicsBeginImageContext( newSize );

        [bottomImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

        [myImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:1.0];

        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil);

        //option if you are in other view when save
        //[self.navigationController popViewControllerAnimated:YES];


    }

You can integrate this code to crop a image:

in InterfaceBuilder using 2 part of image for picking with the specific size you want, for example on iPhone using 2 UIImageView W:150 H:300 total il a 300x300 and using a crop image with size.

CGRect clippedRect  = CGRectMake(self.view.frame.origin.x+91, self.view.frame.origin.y, self.view.frame.size.width-91*2, self.view.frame.size.height-220);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], clippedRect);
UIImage *imageCrop   = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);

Hope this help you

Upvotes: 3

JoshA
JoshA

Reputation: 133

If you actually want to save the images as a single image to a file then look at this post here. You will need a graphics context and all that stuff. Just make sure to set their height, width, and location values appropriately to not overlap.

Otherwise if you just wanted to do this in memory, create a UIView with the dimensions of both images and add the UIImageViews to this UIView. Be sure to set their frames correctly

UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
[container addSubview:image1];
[container addSubview:image2];
image1.frame = CGRectMake(0, 0, 150, 300);
image2.frame = CGRectMake(150, 0, 150, 300);

Then just move around the container or whatever you would like to do with it.

Upvotes: 0

Related Questions