嗨,我看了一下保存图像的例子,然后我只想保存屏幕的一部分。我设法保存了从图像左上角开始的部分,但实际上我想保存屏幕的中心。只保存图像一部分的神奇之处是设置具有一定大小的Graphics上下文,如下所示:
UIGraphicsBeginImageContextWithOptions(CGSizeMake(300, 300), YES, 5.0f);我认为可能有一种方法可以使用CGRect而不是大小,但这给了我一个错误。还有其他的企图或想法吗?我是否需要查看屏幕截图中的像素,获取所需的像素并从中生成新的图像(这将是我所能想到的那种复杂的方式,但也许有一个更简单的方法)?
发布于 2013-10-22 20:59:46
要对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}。C4Image对象实际上有一个renderInContext:方法,因此您不必从图像的层执行此操作。C4Image对象包装了UIImage对象,这就是为什么我们使用从当前上下文获得的UIImage构建一个新对象的原因。发布于 2013-10-21 09:31:41
我为此编写的这个方法非常有效:
+ (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)中创建主视图的printscreen
UIImage* image = [self getTheArea:CGRectMake(100,50,100,100) inView:view];发布于 2013-10-23 07:52:39
稍微修改一下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;
}https://stackoverflow.com/questions/19490227
复制相似问题