为什么我以编程方式创建的屏幕截图在 iOS 7 上看起来如此糟糕?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18956611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does my programmatically created screenshot look so bad on iOS 7?
提问by Yossi
I am trying to implement sharing app with facebook. I used this code to take the screenshot:
我正在尝试与 facebook 实现共享应用程序。我使用此代码截取屏幕截图:
CGSize imageSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height);
UIGraphicsBeginImageContext(imageSize);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
It works great on iOS 6, but in iOS 7 the image looks very bad. I used this answer: iOS: what's the fastest, most performant way to make a screenshot programmatically?for trying to fix it, and it helped, still the screenshot looks bad. The screen gets another color, and some objects (like labels) aren't showing on the image taken. Any help?
它在 iOS 6 上运行良好,但在 iOS 7 中图像看起来很糟糕。我使用了这个答案:iOS:以编程方式制作屏幕截图的最快、最高效的方法是什么?试图修复它,它有帮助,但屏幕截图看起来仍然很糟糕。屏幕获得另一种颜色,并且某些对象(如标签)未显示在拍摄的图像上。有什么帮助吗?
----Update----
- - 更新 - -
I managed to solve the most objects, by change them to retain instead of weak. My main problem remained my tableview, that shown as a big white block (It supposed to be transparent, with labels with white text, so all we see is white cells). I did try to define the table background as clearcolor,not helps..
我设法解决了大多数对象,通过将它们更改为保留而不是弱。我的主要问题仍然是我的 tableview,它显示为一个大的白色块(它应该是透明的,带有白色文本的标签,所以我们看到的只是白色单元格)。我确实尝试将表格背景定义为 clearcolor,没有帮助..
----Last Update---
- - 最后更新 - -
There are wonderful answers here that not really regarding to my issue.. I wanted to make it work on device that runs with iOS7 but without using iOS7 SDK, since it takes to much effort to switch the project SDK in this point, when the project is almost done.
这里有一些很棒的答案,并不是真正与我的问题有关。快完成了。
Anyway, I added the peace of code that finally solved my issue:
无论如何,我添加了最终解决我的问题的代码和平:
This change simply solve the problem:
这个改动简单的解决了这个问题:
UIGraphicsBeginImageContextWithOptions(imageSize, NO , 0.0f);
instead of:
代替:
UIGraphicsBeginImageContext(imageSize);
回答by Vinzzz
New APIhas been added since iOS 7, that should provide efficient way of getting snapshot
自 iOS 7 以来添加了新API,这应该提供获取快照的有效方法
snapshotViewAfterScreenUpdates:
renders the view into a UIView with unmodifiablecontent
resizableSnapshotViewFromRect:afterScreenUpdates:withCapInsets
: same thing, but with resizable insetsdrawViewHierarchyInRect:afterScreenUpdates:
: same thing if you need all subviews to be drawn too (like labels, buttons...)
snapshotViewAfterScreenUpdates:
将视图渲染为不可修改的 UIViewcontent
resizableSnapshotViewFromRect:afterScreenUpdates:withCapInsets
:同样的事情,但具有可调整大小的插图drawViewHierarchyInRect:afterScreenUpdates:
:如果您还需要绘制所有子视图(如标签、按钮...)
You can use the UIView
returned for any UI effect, or render in into an image like you did if you need to export.
您可以使用UIView
返回的任何 UI 效果,或者在需要导出时像您一样渲染到图像中。
I don't know how good this new method performs VS the one you provided (although I remember Apple engineers saying this new API was more efficient)
我不知道这种新方法与你提供的方法相比有多好(虽然我记得苹果工程师说这个新 API 效率更高)
回答by user1526474
you can try this
你可以试试这个
- (UIImage *) screenshot {
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
回答by swiftBoy
in iOS 8: this is how i am doing to get ScreenShot
在 iOS 8 中:这就是我获取 ScreenShot 的方式
- just added one UIImageView and Method to take screenshot in .h file
- 刚刚添加了一个 UIImageView 和 Method 在.h 文件中截取屏幕截图
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
-(IBAction)takeSnapShot:(id)sender;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
-(IBAction)takeSnapShot:(id)sender;
2 added code snip for taking screen shot and set on UIImageView in .m file
2 添加了用于截屏并在 .m 文件中的UIImageView上设置的代码片段
- (IBAction)takeSnapShot:(id)sender
{
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *snapShotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imageView.image = snapShotImage;
}
- below is the output i got.
- 下面是我得到的输出。
回答by ancajic
On iOS7 you can have glitches if you use
在 iOS7 上,如果您使用,您可能会遇到故障
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES]
during ongoing animation. Set afterScreenUpdates = NO
to get rid of glitches.
在正在进行的动画过程中。设置afterScreenUpdates = NO
消除故障。
回答by Bartosz Ciechanowski
Make sure that opaque
is set to NO
确保opaque
设置为NO