문제

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top