1

嘿,我浏览了保存图像的示例,之后我只想保存屏幕的一部分。我设法保存了从图像左上角开始的部分,但我实际上想保存屏幕的中心。只保存图像的一部分的神奇之处在于设置具有一定大小的图形上下文,如下所示:

 UIGraphicsBeginImageContextWithOptions(CGSizeMake(300, 300), YES, 5.0f);

我认为可能有一种方法可以使用 CGRect 而不是大小,但这给了我一个错误。还有其他尝试或想法吗?我是否必须检查屏幕截图的像素,获取所需的像素并从中制作新图像(这将是我能想到的那种复杂的方式,但也许有更简单的方式)?

4

3 回答 3

1

我为此编写的这种方法非常有效:

+ (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;
}

例如,如果您想制作主视图的打印屏幕,在 (100,50,100,100)

UIImage* image = [self getTheArea:CGRectMake(100,50,100,100) inView:view];
于 2013-10-21T09:31:41.863 回答
1

对 Travis 的回答稍作修改,使其独立于图像,但依赖于画布原点:

-(C4Image *)cropImage:(C4Image *)originalImage withOrigin:(CGPoint)origin toArea:(CGRect)rect{
    //grab the image scale
    CGFloat scale = originalImage.UIImage.scale;

    //begin an image context
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, scale);

    //create a new context ref
    CGContextRef c = UIGraphicsGetCurrentContext();

    //shift BACKWARDS in both directions because this moves the image
    //the area to crop shifts INTO: (0, 0, rect.size.width, rect.size.height)
    CGContextTranslateCTM(c, origin.x-rect.origin.x, origin.y-rect.origin.y);

    //render the original image into the context
    [originalImage renderInContext:c];

    //grab a UIImage from the context
    UIImage *newUIImage = UIGraphicsGetImageFromCurrentImageContext();

    //end the image context
    UIGraphicsEndImageContext();

    //create a new C4Image
    C4Image *newImage = [C4Image imageWithUIImage:newUIImage];

    //return the new image
    return newImage;
}
于 2013-10-23T07:52:39.473 回答
1

要使用 C4Image 对象执行此操作,您可以将 incmiko 的答案修改为如下所示:

#import "C4Workspace.h"

@implementation C4WorkSpace{
    C4Image *image;
    C4Image *croppedImage;
}

-(void)setup {
    image=[C4Image imageNamed:@"C4Sky.png"];
    image.origin=CGPointMake(0, 20);

    croppedImage = [self cropImage:image toArea:CGRectMake(150,50,100,100)];
    croppedImage.origin = CGPointMake(20, 360);
    [self.canvas addObjects:@[image,croppedImage]];
}

-(C4Image *)cropImage:(C4Image *)originalImage toArea:(CGRect)rect{
    //grab the image scale
    CGFloat scale = originalImage.UIImage.scale;

    //begin an image context
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, scale);

    //create a new context ref
    CGContextRef c = UIGraphicsGetCurrentContext();

    //shift BACKWARDS in both directions because this moves the image
    //the area to crop shifts INTO: (0, 0, rect.size.width, rect.size.height)
    CGContextTranslateCTM(c, -rect.origin.x, -rect.origin.y);

    //render the original image into the context
    [originalImage renderInContext:c];

    //grab a UIImage from the context
    UIImage *newUIImage = UIGraphicsGetImageFromCurrentImageContext();

    //end the image context
    UIGraphicsEndImageContext();

    //create a new C4Image
    C4Image *newImage = [C4Image imageWithUIImage:newUIImage];

    //return the new image
    return newImage;
}
@end

除了代码中的注释之外,还有其他一些事情需要注意:

  1. 您正在裁剪的“区域”将始终参考您正在裁剪的“图像”。因此,如果您想{150,50}从图像中裁剪,并且图像的原点位于,{20,20}那么它看起来就像您正在{170,70}从 CANVAS 裁剪。

  2. C4Image对象实际上有一个renderInContext:方法,因此您不必从图像层执行此操作。

  3. C4Image对象包装 对象,这就是为什么我们使用从当前上下文中获得的UIImage对象来构建一个新对象UIImage

于 2013-10-22T20:59:46.190 回答