美文网首页
iOS视图生成图片

iOS视图生成图片

作者: 过客又见过客 | 来源:发表于2018-06-16 13:46 被阅读95次

    iOS10以上

     UIGraphicsImageRendererFormat *format = [[UIGraphicsImageRendererFormat alloc] init];
        format.prefersExtendedRange = YES;
        UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:self.imageView.size format:format];
        __weak typeof(self) weakSelf = self;
        UIImage *tmpImage = [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) {
            return [weakSelf.imageView.layer renderInContext:rendererContext.CGContext];
        }];
    

    iOS 10以下的方式

    UIGraphicsBeginImageContextWithOptions(self.selectedImage.size, NO, self.selectedImage.scale);
        [self.selectedImage drawAtPoint:CGPointZero];
        CGFloat scale = self.selectedImage.size.width / self.imageView.width;
        CGContextScaleCTM(UIGraphicsGetCurrentContext(), scale, scale);
        [self.imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *tmpImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    

    可加群一起交流共同学习:801216530。

    相关文章

      网友评论

          本文标题:iOS视图生成图片

          本文链接:https://www.haomeiwen.com/subject/cckbeftx.html