iOS 通过一个View生成一个有透明背景的图片

- (UIImage *)convertImageWithView:(UIView *)convertView

{

    CGFloat scale = [UIScreen mainScreen].scale;

    UIGraphicsBeginImageContextWithOptions(convertView.bounds.size, NO, scale);

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSaveGState(context);

    [convertView.layer renderInContext:context];

    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    

    CGFloat x = convertView.frame.origin.x * scale;

    CGFloat y = convertView.frame.origin.y * scale;

    CGFloat w = convertView.frame.size.width * scale;

    CGFloat h = convertView.frame.size.height * scale;

    CGRect dianRect = CGRectMake(x, y, w, h);

    

    CGImageRef sourceImageRef = [image CGImage];

    CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, dianRect);

    UIImage * newImage = [UIImage imageWithCGImage:newImageRef scale:scale orientation:UIImageOrientationUp];

    return newImage;

}

你可能感兴趣的:(ios)