通过选择另一个UIImage的一部分进行UIImage [英] UIImage by selecting part of another UIImage
问题描述
我有一个UIImage和一个CGPoint告诉我我应该将它移动到什么方向来创建另一个图像。背景可以是任何东西。
给出最初的UIImage如何创建新的UIImage?最有效的方法是什么?
这是我正在做的事情:
int originalWidth = image.size.width;
int originalHeight = image.size.height;
float xDifference = [[coords objectAtIndex:0] floatValue];
float yDifference = [[coords objectAtIndex:1] floatValue];
UIView * tempView = [[UIView alloc] initWithFrame:CGRectMake(0,240,originalWidth,originalHeight)];
UIImageView * imageView = [[UIImageView alloc] initWithImage:image];
imageView.contentMode = UIViewContentModeTopLeft;
CGRect imageFrame = imageView.frame;
imageFrame.origin.x = xDifference;
imageFrame.origin.y = -yDifference;
imageView.frame = imageFrame;
UIGraphicsBeginImageContext(tempView.bounds.size);
[tempView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
是否有更优的版本?
您可以使用 很好地介绍了如何使用这个属性。
I have an UIImage and a CGPoint which tells me in what direction I should move it to create another image. The background can be anything.
Give the initial UIImage how I can create the new one? What is the most efficient way of doing it?
Here is what I'm doing:
int originalWidth = image.size.width;
int originalHeight = image.size.height;
float xDifference = [[coords objectAtIndex:0] floatValue];
float yDifference = [[coords objectAtIndex:1] floatValue];
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 240, originalWidth, originalHeight)];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.contentMode = UIViewContentModeTopLeft;
CGRect imageFrame = imageView.frame;
imageFrame.origin.x = xDifference;
imageFrame.origin.y = -yDifference;
imageView.frame = imageFrame;
UIGraphicsBeginImageContext(tempView.bounds.size);
[tempView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Is there a more optimal version?
You can use CGImageCreateWithImageInRect(). You can get a CGImage
from a UIImage
with the property of the same name.
Basically what you end up doing is apply masks to the existing image to extract the portions you need. Like so -
myImageArea = CGRectMake(xOrigin, yOrigin, myWidth, myHeight);//newImage
mySubimage = CGImageCreateWithImageInRect(oldImage, myImageArea);
myRect = CGRectMake(0, 0, myWidth*2, myHeight*2);
CGContextDrawImage(context, myRect, mySubimage);
This post gives a good idea of how to use this property.
这篇关于通过选择另一个UIImage的一部分进行UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!