无法在NewPad 9.3.5上将UIView转换为UIImage

时间:2017-03-18 11:10:49

标签: ios objective-c uiview uiimage

使用AVCaptureVideoPreviewLayer从包含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]; 的UIView快照,然后我想将结果(UIView *)转换为UIImage,然后显示,但UIImage一直是空的


Xcode Version 8.2.1 (8C1002)
MacOS Sierra 10.12.3(16D32)
Deployment Target 9.3
iPad iOS 9.3.5

有谁知道为什么无法获得正确的UIImage?

>>> from bidict import bidict >>> element_by_symbol = bidict(H='hydrogen') >>> element_by_symbol bidict({'H': 'hydrogen'}) >>> element_by_symbol['H'] 'hydrogen' >>> element_by_symbol.inv bidict({'hydrogen': 'H'}) >>> element_by_symbol.inv['hydrogen'] 'H' >>> element_by_symbol.inv.inv is element_by_symbol True

2 个答案:

答案 0 :(得分:-1)

我不知道这个的Objective-C版本,但是尝试自己将它转换为Obj-C,并添加以下代码:

let context = UIGraphicsGetCurrentContext()
photo.layer.render(in:context!)

之后添加

[photo drawViewHierarchyInRect:photo.bounds afterScreenUpdates:YES];

但之前

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

答案 1 :(得分:-1)

使用以下方法(未经测试):

- (UIImage *)renderViewToImage:(UIView *)view
{
    UIImage* image = nil;

    UIGraphicsBeginImageContext(view.bounds.size);          
    [view.layer renderInContext: UIGraphicsGetCurrentContext()];        
    image = UIGraphicsGetImageFromCurrentImageContext();            
    UIGraphicsEndImageContext();

    return image
}

并致电:

UIImage *img = [self renderViewToImage:photo]

您可能希望将其设为UIView的分类。