首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C4保存图像的部分

C4保存图像的部分
EN

Stack Overflow用户
提问于 2013-10-21 09:05:24
回答 3查看 103关注 0票数 1

嗨,我看了一下保存图像的例子,然后我只想保存屏幕的一部分。我设法保存了从图像左上角开始的部分,但实际上我想保存屏幕的中心。只保存图像一部分的神奇之处是设置具有一定大小的Graphics上下文,如下所示:

代码语言:javascript
复制
 UIGraphicsBeginImageContextWithOptions(CGSizeMake(300, 300), YES, 5.0f);

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-10-22 20:59:46

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

代码语言:javascript
复制
#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}
  2. C4Image对象实际上有一个renderInContext:方法,因此您不必从图像的层执行此操作。
  3. C4Image对象包装了UIImage对象,这就是为什么我们使用从当前上下文获得的UIImage构建一个新对象的原因。
票数 1
EN

Stack Overflow用户

发布于 2013-10-21 09:31:41

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

代码语言:javascript
复制
+ (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

代码语言:javascript
复制
UIImage* image = [self getTheArea:CGRectMake(100,50,100,100) inView:view];
票数 1
EN

Stack Overflow用户

发布于 2013-10-23 07:52:39

稍微修改一下Travis的答案,就可以使其独立于图像,但取决于画布的来源:

代码语言:javascript
复制
-(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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19490227

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档