Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 在屏幕抓取中保持图像分辨率_Iphone_Ios_Cocoa Touch_Uiimage_Uigraphicscontext - Fatal编程技术网

Iphone 在屏幕抓取中保持图像分辨率

Iphone 在屏幕抓取中保持图像分辨率,iphone,ios,cocoa-touch,uiimage,uigraphicscontext,Iphone,Ios,Cocoa Touch,Uiimage,Uigraphicscontext,在我的应用程序中,用户可以在照片上粘贴标签。当他们去保存他们的创作时,我抓取屏幕并将其存储在UIImage: UIGraphicsBeginImageContextWithOptions(self.mainView.bounds.size, NO, [UIScreen mainScreen].scale); [self.mainView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *resultImage = [UI

在我的应用程序中,用户可以在照片上粘贴标签。当他们去保存他们的创作时,我抓取屏幕并将其存储在
UIImage

UIGraphicsBeginImageContextWithOptions(self.mainView.bounds.size, NO, [UIScreen mainScreen].scale);
[self.mainView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultImage = [UIGraphicsGetImageFromCurrentImageContext() retain];
UIGraphicsEndImageContext();
(其中,
self.mainView
有一个子视图
UIImageView
保存照片,另一个子视图
UIView
保存标签)


我想知道,是否可以以这种方式进行屏幕截图,并保持上述照片的分辨率?

如果您希望保存当前视图的图像,那么这可能会对您有所帮助

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);

以下操作将把两个
UIImage
s“展平”为一个,同时保持原始图像的分辨率:

以上假设
photoImage
stickerImage
都是
UIImage
的实例,
stickerView
是包含
stickerImage
UIView
视图,因此能够使用
stickerView
框架确定其原点

如果您有多个标签,只需在集合中迭代即可

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();