C4 saving part of an image

后端 未结 3 535
执笔经年
执笔经年 2021-01-27 01:53

Hei, I went through the example for saving images and afterwards I wanted to save only a part of the screen. I managed to save the part starting at the upper left corner of the

3条回答
  •  攒了一身酷
    2021-01-27 02:41

    This method what I wrote for this is works perfectly:

    + (UIImage*) getTheArea:(CGRect)area inView:(UIView*)view{
    
        if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
            UIGraphicsBeginImageContextWithOptions(CGSizeMake(area.size.width, area.size.height), NO, [UIScreen mainScreen].scale);
        else
            UIGraphicsBeginImageContext(view.bounds.size);
    
        CGContextRef c = UIGraphicsGetCurrentContext();
        CGContextTranslateCTM(c, -area.origin.x, -area.origin.y);    // <-- shift everything up by 40px when drawing.
        [view.layer renderInContext:c];
        UIImage* thePrintScreen = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return thePrintScreen;
    }
    

    for example, if you want to make a printscreen of your main view, in (100,50,100,100)

    UIImage* image = [self getTheArea:CGRectMake(100,50,100,100) inView:view];
    

提交回复
热议问题