[英]How to capture the contents of a UIView and change it into a UIImage?
我目前有一個視圖,我想將其更改為UIImage。 我想這樣做是因為UIImage類對於我需要做的事情要好得多。 你如何捕獲UIView的內容並將內容復制到UIImage中?
謝謝,
-大衛
像這樣:
UIGraphicsBeginImageContext(myView.bounds.size);
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *myImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
您需要包含CoreGraphics框架,並導入CALayer.h:
#import <QuartzCore/CALayer.h>
在這里,試試這個
CGImageRef screen = UIGetScreenImage();
UIImage *screenImage = [UIImage imageWithCGImage:screen];
這將截取屏幕截圖,因此理論上捕獲所有視圖的元素,並為您提供UIImage。 希望有所幫助!
將以下方法添加到UIView類別並使用
- (UIImage*) capture {
UIGraphicsBeginImageContext(self.bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.