平, 我通过示例保存图像,然后我只想保存屏幕的一部分。 我设法保存从图像左上角开始的部分,但实际上我想保存屏幕的中心。 仅保存图像的一部分的神奇之处在于设置具有特定大小的图形上下文,如下所示:
UIGraphicsBeginImageContextWithOptions(CGSizeMake(300, 300), YES, 5.0f);
我认为可能有一种方法可以使用CGRect而不是大小,但这会给我一个错误。任何其他尝试或想法?我是否必须浏览屏幕截图的像素,抓住所需的图像并从中创建一个新图像(这可能是我能想到的那种复杂的方式,但也许更简单一种)?
答案 0 :(得分: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];
答案 1 :(得分: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
除了代码中的注释之外,还有其他一些需要注意的事项:
您正在裁剪的“区域”将始终参考您正在裁剪的“图像”。因此,如果您想从图片中裁剪{150,50}
,并且图片的原点位于{20,20}
,那么它就会像您从CANVAS裁剪{170,70}
一样。
C4Image
对象实际上有renderInContext:
方法,因此您无需从图像的图层执行此操作。
C4Image
个对象 wrap UIImage
个对象,这就是我们使用从当前上下文获取的UIImage
构建一个新对象的原因< / p>
答案 2 :(得分: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;
}