使用- (nullable UIView *)resizableSnapshotViewFromRect:(CGRect)rect afterScreenUpdates:(BOOL)afterUpdates withCapInsets:(UIEdgeInsets)
從包含AVCaptureVideoPreviewLayer
的UIView獲得快照,然後我想將結果(UIView *)轉換爲UIImage並顯示,但UIImage是空的每時每刻。無法在U盤上將UIView轉換爲UIImage 9.3.5
CGRect frame = CGRectMake(0, 0, 768, 893);
UIView *photo = [self.cameraView resizableSnapshotViewFromRect:frame afterScreenUpdates:YES withCapInsets:UIEdgeInsetsZero];
UIGraphicsBeginImageContextWithOptions(photo.bounds.size, NO, [[UIScreen mainScreen] scale]);
[photo drawViewHierarchyInRect:photo.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView * iv = [[UIImageView alloc] initWithImage:image];
[iv setFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
[self addSubview:iv];
CALayer * layer = [iv layer];
layer.borderWidth = 2.0f;
layer.borderColor = [[UIColor orangeColor] CGColor];
有沒有人知道爲什麼不能得到正確的UIImage?
Xcode Version 8.2.1 (8C1002) MacOS Sierra 10.12.3(16D32) Deployment Target 9.3 iPad iOS 9.3.5
'[photo.layer renderInContext:UIGraphicsGetCurrentContext()]'不起作用 – leezhm
@leezhm嗯..我認爲它會工作..你確定你的UIView已被繪製?你在那個draw-function中發送'afterScreenUpdates:YES',你知道這個代碼的含義嗎?我不..也許它會在繪製視圖之前等待屏幕準備就緒,但其餘代碼會立即顯示出來。嘗試將其更改爲「否」? – Sti
嘗試是和否,它不起作用!我的項目是一個iPad應用程序,我發現這個話題https://github.com/jfradj/ScreenshotIssue http://stackoverflow.com/questions/25432222/drawviewhierarchyinrectafterscreenupdates-and-renderincontext-dont-work-ot – leezhm