将UIImageView图层导出到UIImage

时间:2016-11-09 01:24:13

标签: ios swift uiimageview uiimage

我有一个UIImageView,上面有一层,我想用这些图层保存UIImageView.image。

示例:

imageView.layer.mask = someLayer

let image = imageView.image

let activity = UIActivityViewController(activityItems: [image], applicationActivities: nil)
        present(activity, animated: true, completion: nil)

但是这只保存图像而没有来自imageView的图层。我该怎么办?

截取imageView的屏幕截图可能不错,但是在执行此操作时我会丢失图像质量,我想保留原始分辨率。

1 个答案:

答案 0 :(得分:-1)

这将为您提供原始分辨率:

- (UIImage *)imageFromImageView:(UIImageView *)view{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale);
    [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}