本文介绍了将UIscrollView保存到Uiimage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码将编辑后的UIscrollView保存到UIImage中.但是我在视图中看到的不是我保存的.在某种程度上,已保存的照片的侧面被空白的灰色条抵消了.我的UIscrollView没有碰到视图的框架.任何人都知道如何去做吗?如果我必须使用"theScrollView.layer"来呈现它.放大照片后,照片会变差.

  CGSize photoSize = theScrollView.bounds.size;UIGraphicsBeginImageContext(photoSize);CGContextRef context = UIGraphicsGetCurrentContext();CALayer * layer = self.view.layer;[layer renderInContext:context];UIImage * someImage = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();UIImageWriteToSavedPhotosAlbum(someImage,nil,nil,nil); 
解决方案

替换 CGContextRef context = UIGraphicsGetCurrentContext();

如果([UI的主屏幕[responseToSelector:@selector(scale)]))UIGraphicsBeginImageContextWithOptions(self.PostView.bounds.size,NO,[UIScreen mainScreen] .scale);别的UIGraphicsBeginImageContext(self.PostView.bounds.size);

如果您从Retina Display捕获,这可能会有所帮助.

  • 如果您需要截取uiscrollview的屏幕截图,请将"self.view.layer"替换为"yourscrollview.layer"

祝你好运

I am trying to save the edited UIscrollView into UIImage using following code. But what I see in the view is not what I saved. It was somehow offset with a blank grey color bar at the side of the saved photo. My UIscrollView is not touching the frame of the view. Anyone have idea on how to go about? If I have to use "theScrollView.layer" to render it. The picture will get worst after I zoom in the photo.

CGSize photoSize = theScrollView.bounds.size;
UIGraphicsBeginImageContext(photoSize);
CGContextRef context = UIGraphicsGetCurrentContext();
CALayer *layer = self.view.layer;
[layer renderInContext:context];
UIImage *someImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(someImage,nil, nil, nil);
解决方案

replace CGContextRef context = UIGraphicsGetCurrentContext(); with

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) UIGraphicsBeginImageContextWithOptions(self.PostView.bounds.size, NO, [UIScreen mainScreen].scale); else UIGraphicsBeginImageContext(self.PostView.bounds.size);

This may help if u capture from Retina Display.

  • if u need to take screenshot of uiscrollview replace 'self.view.layer' with 'yourscrollview.layer'

Good Luck

这篇关于将UIscrollView保存到Uiimage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 23:30