How to create an image from a UIView / UIScrollView

后端 未结 1 711
小蘑菇
小蘑菇 2020-12-08 17:28

I have an image in an UIScrollView, that can be scrolled and zoomed.

When the user presses a button, I want the code to create an image from whatever part of the UIS

相关标签:
1条回答
  • 2020-12-08 18:12

    I've managed to get it.

    Here's my solution, based on a few different ones from the web:

    - (UIImage *)imageByCropping:(UIScrollView *)imageToCrop toRect:(CGRect)rect
    {
        CGSize pageSize = rect.size;
        UIGraphicsBeginImageContext(pageSize);
    
        CGContextRef resizedContext = UIGraphicsGetCurrentContext();
        CGContextTranslateCTM(resizedContext, -imageToCrop.contentOffset.x, -imageToCrop.contentOffset.y);
        [imageToCrop.layer renderInContext:resizedContext];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
        UIGraphicsEndImageContext();
    
        return image;
    }
    

    which you call by using:

    CGRect clippedRect = CGRectMake(0, 0, 320, 300);
    picture.image = [self imageByCropping:myScrollView toRect:clippedRect];
    
    0 讨论(0)
提交回复
热议问题