xcode iPhone:如何绘制一幅图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2670733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:17:19  来源:igfitidea点击:

iPhone: How can you draw a piece of an image

iphonexcodeimagedrawrect

提问by Mark

Code sample

代码示例

- (void)drawRect:(CGRect)rect {  
    [super drawRect:rect];

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSaveGState(context);
    CGContextTranslateCTM(context, 0, self.frame.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    CGContextDrawImage(context, CGRectMake(0.0, 0.0, self.frame.size.width, self.frame.size.height), [UIImage imageNamed:@"sample.png"].CGImage);

    CGContextRestoreGState(context);    
}

================

================

I would like to copy a certain rect within an image to the context, so not the entire image is drawn but just a piece of the image. Does anyone have a solution for this? I can't find anything on google nor the documentation.

我想将图像中的某个矩形复制到上下文中,因此不会绘制整个图像而只是图像的一部分。有没有人对此有解决方案?我在谷歌和文档上都找不到任何东西。

I know there are alternatives like: 1. Create a UIView with clipping and then just position the UIImageView within it. 2. Create the UIImageView within the UIScrollView and use content offset.

我知道有一些替代方法,例如: 1. 创建一个带有剪辑的 UIView,然后将 UIImageView 放置在其中。2. 在 UIScrollView 中创建 UIImageView 并使用内容偏移。

But I think those are lame...

但是我觉得这些都是废话...

回答by James

CGImageCreateWithImageInRect should work fine. That's how I do it. Here's an example:

CGImageCreateWithImageInRect 应该可以正常工作。我就是这样做的。下面是一个例子:

CGImageRef ref = CGImageCreateWithImageInRect(some_UIImage.CGImage, CGRectMake(x, y, height, width));
UIImage *img = [UIImage imageWithCGImage:ref];

This example gets a CGImageRef from the CGImage property of another_UIImage, an instance of UIImage, then crops it, and turns it back into a UIImage with UIImage's imageWithCGImage constructor method. Good luck!

本示例从 another_UIImage 的 CGImage 属性(UIImage 的一个实例)中获取一个 CGImageRef,然后将其裁剪,并使用 UIImage 的 imageWithCGImage 构造函数将其转换回 UIImage。祝你好运!

James

詹姆士

回答by adam

You could try CGImageCreateWithImageInRect

你可以试试 CGImageCreateWithImageInRect

回答by Mark

This did work:

这确实有效:

UIGraphicsBeginImageContext(rect.size);
CGContextRef currentContext = UIGraphicsGetCurrentContext();

CGRect clippedRect = CGRectMake(0, 0, rect.size.width, rect.size.height);
CGContextClipToRect( currentContext, clippedRect);

CGRect drawRect = CGRectMake(rect.origin.x * -1,
                             rect.origin.y * -1,
                             imageToCrop.size.width,
                             imageToCrop.size.height);

CGContextDrawImage(currentContext, drawRect, imageToCrop.CGImage);

UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();