C4保存图像的一部分

嘿,我经历了保存图像的例子,之后我只想保存屏幕的一部分。 我设法保存从图像左上angular开始的部分,但我实际上想保存我的屏幕的中心。 仅保存图像的一部分的魔法是设置具有一定大小的graphics上下文,如下所示:

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

我认为可能有一种方法来使用CGRect而不是大小,但是这给了我一个错误。 任何其他的尝试或想法? 我是否需要通过截图的像素来抓取所需的图像,并创build一个新的图像(这可能是我能想到的那种复杂的方式,但也许有一个更简单的方法)?

要使用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. 您裁剪的“区域”将始终参考您裁剪的“图像”。 所以,如果你想从图像中裁剪{170,70} ,并且图像的原点在{20,20}那么就会看起来像你从帆布剪裁{170,70}

  2. C4Image对象实际上有一个renderInContext:方法,所以你不必从图像的图层做到这一点。

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

这个方法我写的是完美的作品:

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

对Travis的答案做了一点修改,使得它独立于图像,但依赖于canvas的起源:

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