如何捕获UIView的内容并将其更改为UIImage?

bob*_*age 5 iphone objective-c

我目前有一个视图,我想将其更改为UIImage.我想这样做是因为UIImage类对于我需要做的事情要好得多.你如何捕获UIView的内容并将内容复制到UIImage中?

谢谢,

-大卫

v01*_*01d 7

像这样:

UIGraphicsBeginImageContext(myView.bounds.size);
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *myImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)

您需要包含CoreGraphics框架,并导入CALayer.h:

#import <QuartzCore/CALayer.h>
Run Code Online (Sandbox Code Playgroud)