Combining multiple UIImageView and maintaining resolution

I am developing an application in which a user places several UIImageViews one above the other. When the user decides to save this in the photo album, I need to combine all these UIImageView and save it in the Photo Library. Connecting them, I need to keep my positions, permissions, zorder. The approach I tried was to have a parent UIView that acts like a container. The user places all the UIImageView inside this UIView. While saving, I take a screenshot of the UIView and save it. Although this approach works, it does not preserve resolution. The resolution of the final image is the same as the resolution of the parent size of the UIView (width and height 300 pixels). Is there a way to keep the resolution? or at least have a higher resolution, for example, up to 1024 x 768 pixels? Any code examples / examples would be appreciated!

+2
iphone uiimage uiimageview
source share
1 answer

What you do while maintaining a UIView is a very good idea if the size of the UIView is higher than the size of the largest image you have, but it almost never happens. You need to create a graphics context (CGContextRef), the size of the largest of your images, sort the images in z-order and start drawing these images there. I will try to help you with some pseudo code:

-(UIImage *)mergeFlag{ UIImage * mergedImage = nil; //In some pace you need to fill these values with the biggest width of all your images and the biggest height; int Max_Width; int Max_Height; //Use here your image with the biggest Z-order. CGImageRef image; size_t cWitdh = Max_Width; size_t cHeight = Max_Height; size_t bitsPerComponent = 8;//If 8 isn't right you should use CGImageGetBitsPerComponent(image) with some image. size_t bytesPerRow = 4 * Max_Width;// I use 4 assuming you have 8 Bits per component and you have 4 components (R,G,B,A) , if you have an image specifically, you can use CGImageGetBytesPerRow(image) //Now we build a Context with those dimensions. CGContextRef context = CGBitmapContextCreate(nil, cWitdh, cHeight, bitsPerComponent, bytesPerRow, CGColorSpaceCreateDeviceRGB(), CGImageGetBitmapInfo(image)); //The code of this cycle is to ilustrate what you have to do: for(image in your Images ordered by z-Order) { //The location where you draw your image on the context is not always the same location you have in your UIView, //this could change and you need to calculate that position according to the scale between you images real size, and the size of the UIImage being show on the UIView. CGContextDrawImage(context, CGRectMake(currentImage.frame.origin.x, currentImage.frame.origin.y, CGImageGetWidth(currentImage), CGImageGetHeight(currentImage), currentImage); } CGImageRef mergeResult = CGBitmapContextCreateImage(context); mergedImage = [[UIImage alloc] initWithCGImage:tmp]; CGContextRelease(context); CGImageRelease(mergeResult); return mergedImage;} 

Hope this helps.

+2
source share

All Articles