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

Categories

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

ios7 - UIImage from a UIView created with snapshotViewAfterScreenUpdates:

Is it possible to get a UIImage from a UIView created with snapshotViewAfterScreenUpdates?

A UIView returned from snapshotViewAfterScreenUpdates looks fine when added as a subview, but the following produces a black image:

UIView *snapshotView = [someView snapshotViewAfterScreenUpdates:YES]; 
UIImage *snapshotImage = [self imageFromView:snapshotView]; 

- (UIImage *)imageFromView:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0.0);
    // [view.layer renderInContext:UIGraphicsGetCurrentContext()]; // <- same result...
    [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return img;
}

It is of course possible to get the image without relying on snapshotViewAfterScreenUpdates:

UIImage *snapshotImage = [self imageFromView:someView];

Unfortunately, when capturing a complex view, drawViewHierarchyInRect can't match the speed of snapshotViewAfterScreenUpdates. I was hoping that it would be faster to get the UIImage from a view created by snapshotViewAfterScreenUpdates, is this possible?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

The answer seems to be NO and Apple's documentation implicitly confirms this:

If you want to apply a graphical effect, such as blur, to a snapshot, use the drawViewHierarchyInRect:afterScreenUpdates: method instead.

It's worth noting that the Implementing Engaging UI on iOS session from WWDC13 lists snapshotViewAfterScreenUpdates as the fastest method, but uses drawViewHierarchyInRect in the example code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...