嘿,
我浏览了保存图像的示例,然后我只想保存屏幕的一部分。
我设法保存从图像左上角开始的部分,但实际上我想保存屏幕的中心。
仅保存图像的一部分的神奇之处在于设置具有特定大小的图形上下文,如下所示:
UIGraphicsBeginImageContextWithOptions(CGSizeMake(300, 300), YES, 5.0f);
我认为可能有一种方法可以使用 CGRect 而不是大小,但这给了我一个错误。还有其他尝试或想法吗?我是否必须检查屏幕截图的像素,抓住所需的像素并从中制作新图像(这将是我能想到的一种复杂的方法,但也许有更简单的方法)?
要使用 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}
那么它看起来就像你正在裁剪{170,70}
来自画布。
The C4Image
对象实际上有一个renderInContext:
方法,因此您不必从图像层执行此操作。
C4Image
物体wrap UIImage
对象,这就是为什么我们使用UIImage
我们从当前上下文中得到的
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)