Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
933 views
in Technique[技术] by (71.8m points)

objective c - Why does my programmatically created screenshot look so bad on iOS 7?

I am trying to implement sharing app with facebook. I used this code to take the screenshot:

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?

----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..

----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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

New API has been added since iOS 7, that should provide efficient way of getting snapshot

  • snapshotViewAfterScreenUpdates: renders the view into a UIView with unmodifiable content

  • resizableSnapshotViewFromRect:afterScreenUpdates:withCapInsets : same thing, but with resizable insets

  • drawViewHierarchyInRect:afterScreenUpdates: : same thing if you need all subviews to be drawn too (like labels, buttons...)

You can use the UIView returned for any UI effect, or render in into an image like you did if you need to export.

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...