Merge two images into single image in iphone - iphone

Here is my xib files look like below
My 2 imageviews are added to view which different than view of xib file.
I need to merge images from 2 imageview into single image. BUT MY PROBLEM IS MY SECOND IMAGEVIEW WHICH CONTAINS "blur.png" IS DRAGGABLE. My second imageview is smaller than first imageview. How can I merge these images into one & use the final image.
Here is my code
UIGraphicsBeginImageContext(self.viewForImages.bounds.size);
CGRect rect = CGRectMake(0, 0, imageView.image.size.width, imageView.image.size.height);
[imageView.image drawInRect:rect];
[blurImageView.image drawInRect:rect blendMode:kCGBlendModeScreen alpha:1.0];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Thanks.

Related

Transform affect autoresizemask uiimageview

I'm working on an iOS photo app and I have met some problems.
I have a main UIImageView with multiple image subviews, all subviews have autoresizingMask ON.
I can rotate, scale any subviews from main imageView and then I want to save an image from main UIImageView with all it's subviews.
There is the code that I use:
CGRect temp = editedImage.frame;
CGRect temp2 = editedImage.frame;
temp2.size = editedImage.image.size;
editedImage.frame = temp2;
editedImage.backgroundColor = [UIColor clearColor];
UIGraphicsBeginImageContext(editedImage.frame.size);
[editedImage.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
editedImage.frame = temp;
editedImage - is the main UIImageView with many subviews.
Problem: If I rotate a subview from editedImage and after execute the code above then my subview becomes smaller, and each time I do that it becomes smaller and smaller.
Could someone give me an advice why this happens or to give some suggestions how I can solve this?

Maintain image resolution in screen grab

In my app, the user is able to put stickers on top of a photo. When they go to save their creation, I do a screen grab and store it in a UIImage:
UIGraphicsBeginImageContextWithOptions(self.mainView.bounds.size, NO, [UIScreen mainScreen].scale);
[self.mainView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultImage = [UIGraphicsGetImageFromCurrentImageContext() retain];
UIGraphicsEndImageContext();
(where self.mainView has a subview UIImageView which holds the photo, and another subview UIView which holds the stickers).
I am wondering, is it possible to do a screen shot in this manner, and maintain the resolution of the aforementioned photo?
The following will 'flatten' two UIImages into one while maintaining the resolution of the original image(s):
CGSize photoSize = photoImage.size;
UIGraphicsBeginImageContextWithOptions(photoSize, NO, 0.0);
CGRect photoRect = CGRectMake(0, 0, photoSize.width, photoSize.height);
// Add the original photo into the context
[photoImage drawInRect:photoRect];
// Add the sticker image with its upper left corner set to where the user placed it
[stickerImage drawAtPoint:stickerView.frame.origin];
// Get the resulting 'flattened' image
UIImage *flattenedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
The above assumes photoImage and stickerImage are both instances of UIImage and stickerView is a UIView with containing the stickerImage and thus will be able to use the stickerView frame to determine its origin.
If you have multiple stickers, just iterate through the collection.
If you are looking to save an image of your current view then this might help you.
UIGraphicsBeginImageContext(self.scrollView.contentSize);
[self.scrollView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef imageRef = CGImageCreateWithImageInRect(finalImage.CGImage,
CGRectMake(scrollView.contentOffset.x, scrollView.contentOffset.y,
scrollView.frame.size.width, scrollView.frame.size.height));
UIImage *screenImage = [UIImage imageWithCGImage:imageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
CGImageRelease(imageRef);

Building an Image From Multiple UIImageViews

I am interested in building a Collage style app for the IPhone and IPad. I have been looking at other apps and trying to work out the method that they use to create the final image.
I am guessing that the app has a UIView and then each individual image is a UIImageView and is a subview of the main UIView. The user is then able to move each UIImageView and position them as they intend.
Now my question is, after the user has positioned all the UIImageViews on screen how do you then create the final image from what is on the screen. This of course takes into account that you will be upscaling the images to create lets say an A4 sized final print.
Can anyone share some information?
Thanks
You should take a look at CGContext Reference. You can do something like the following to combine imageView 1 and 2 to imageView 3.
Sample Code :
UIGraphicsBeginImageContext(imageView1.image.size);
CGRect rect = CGRectMake(0, 0, imageView1.image.size.width, imageView1.image.size.height);
[imageView1.image drawInRect:rect];
[imageView2.image drawInRect:rect blendMode:kCGBlendModeScreen alpha:0.5];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[imageView3 setImage:resultingImage];
GoodLuck !!!
If you use an UIView to include more 2 Images to merge, that you can try to capture the view to be an image to use.
//Capture View to Image
-(UIImage *)captureImageFromView
{
UIView *theView = self; //self extends UIView
if( NULL != UIGraphicsBeginImageContextWithOptions )
{
UIGraphicsBeginImageContextWithOptions(theView.bounds.size, NO, 2.0f);
}
else
{
//iOS 4.0
UIGraphicsBeginImageContext(theView.frame.size);
}
[theView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *captureImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return captureImage;
}
//Merge 2 Images to be an Image.
-(UIImage *)mergeBaseImage:(UIImage *)_baseImage
underImage:(UIImage *)_underImage
matchBaseImageSize:(BOOL)_matchBaseImageSize
{
UIGraphicsBeginImageContext(_baseImage.size);
[_baseImage drawInRect:CGRectMake(0, 0, _baseImage.size.width, _baseImage.size.height)];
if( _matchBaseImageSize )
{
[_underImage drawInRect:CGRectMake(0, 0, _baseImage.size.width, _baseImage.size.height)];
}
else
{
[_underImage drawInRect:CGRectMake(0, 0, _underImage.size.width, _underImage.size.height)];
}
UIImage *_mergedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return _mergedImage;
}
Wish it can help you.

When taking snap shot there is a showing very little bit white background color

There are Two View controllers in one view controller i am taking an snap shot and in the anothe view controller i am showing tha anap shot that was picked.
I am working on image, i need to take a snap shot while i am doing this i am getting white background. I do not know why this appears? As below image (There are Three image views , Initially image view has a transparent image view then background body image view and tattoo image view.)
When i am taking the screen shot I am hiding the Background image.
i set the view alpha to 1.0f, i set the view background color to clear color
The output screen shot is
My code is
self.backgroundImgView.hidden=YES;
self.view.backgroundColor=[UIColor clearColor];
self.view.alpha=1.0f;
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGRect rect;
rect = CGRectMake(appDelegate.xFloat, appDelegate.yFloat, appDelegate.widthFloat, appDelegate.heightFloat);
CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], rect);
UIImage *img = [UIImage imageWithCGImage:imageRef];
appDelegate.tattooImg=img;
CGImageRelease(imageRef);
Make a UIView like a holderview.... on the self.view. Then add both the images on the holderview not the self.view.... then wen u save the screenshot take it of the holderview ...

Saving two Overlapping UIImage

I am trying to build a photo frame application on iphone. I made the frame it is transparent in png formate, then by choosing photos and was placed behind the frame layer in the interface builder.
In interface builder they are placed well and fit well. Now my problem is how can i save them into one picture.
Here is the code i have, but the saving part keep crashing.
-(IBAction) saveImage:(id)sender{
imagefront .backgroundColor = [UIColor clearColor]; //This sets your backgroung to transparent.
imagefront.opaque = NO;
[imageView bringSubviewToFront:imagefront];
UIImage *overlappedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(overlappedImage, self, #selector(imageSavedToPhotosAlbum: didFinishSavingWithError: contextInfo:), nil);
}
Imagefront is the photoframe while imageView is the photo.
Thank you.
Your current approach is incorrect. You will need to do this to get the image.
UIGraphicsBeginImageContext(imageView.frame.size);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsGetCurrentContext();
This is assuming that imageView has imageFront as its subview as suggested by the code you've posted.