C4 saving part of an image(C4 保存图像的一部分)
问题描述
嘿,我浏览了保存图像的示例,之后我只想保存屏幕的一部分.我设法保存了从图像左上角开始的部分,但我实际上想保存屏幕的中心.只保存图像的一部分的神奇之处在于设置具有一定大小的图形上下文,如下所示:
Hei, I went through the example for saving images and afterwards I wanted to save only a part of the screen. I managed to save the part starting at the upper left corner of the image but I actually want to save the center of my screen. The magic to save only a part of an image is setting up the Graphics Context with a certain size, like this:
UIGraphicsBeginImageContextWithOptions(CGSizeMake(300, 300), YES, 5.0f);
我认为可能有一种方法可以使用 CGRect 而不是大小,但这给了我一个错误.还有其他尝试或想法吗?我是否必须检查屏幕截图的像素,获取所需的像素并从中制作新图像(这是我能想到的那种复杂的方法,但也许有更简单的方法)?
I thought there might be a way to use a CGRect instead of the size, but that gives me an error. any other attempts or thoughts? Do I have to go through the pixels of my screenshot, grab those needed and make a new image out of that (that would be the kind of complicated way I can think of but maybe there is an easier one)?
推荐答案
要使用 C4Image 对象执行此操作,您可以修改 incmiko 的答案,如下所示:
To do this with C4Image objects, you can modify incmiko's answer to look like the following:
#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
除了代码中的注释之外,还有一些其他的事情需要注意:
Aside from the comments in the code there are a couple other things to be aware of:
您正在裁剪的区域"将始终参考您正在裁剪的图像".因此,如果您想从图像中裁剪
{150,50}
,并且图像的原点位于{20,20}
,那么它看起来就像您正在裁剪 <代码>{170,70} 来自 CANVAS.
The "area" you're cropping will always be in reference to the "image" that you're cropping. So, if you want to crop
{150,50}
from the image, and the image's origin is at{20,20}
then it will LOOK like you are cropping{170,70}
from the CANVAS.
C4Image
对象实际上有一个renderInContext:
方法,因此您不必从图像层执行此操作.
The C4Image
object actually has a renderInContext:
method, so you don't have to do this from the image's layer.
C4Image
对象 wrap UIImage
对象,这就是我们使用 UIImage
我们从当前上下文中得到的
C4Image
objects wrap UIImage
objects, which is why we build a new one using the UIImage
that we get from the current context
这篇关于C4 保存图像的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C4 保存图像的一部分
基础教程推荐
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01