[英]How to clip image in scale in iPhone?
I have a large sized image (2048*2048px), this image is shown as 320*320 on iPhone screen. 我有一个大尺寸的图片(2048 * 2048px),该图片在iPhone屏幕上显示为320 * 320。 I want to do this:
我想做这个:
In my APP, user can open large sized image(eg 2048*2048), the image is shown as 320*320 on iPhone screen, and there is rectangle over the image, user can move the rectangle anywhere within image on iPhone screen, eg rectangle(100, 100, 300, 200), then I want to clip the original sized image within the rectangle area in scale. 在我的APP中,用户可以打开大尺寸图像(例如2048 * 2048),iPhone屏幕上的图像显示为320 * 320,图像上方有矩形,用户可以将矩形移动到iPhone屏幕上图像内的任何位置,例如矩形(100,100,300,200),然后我想在矩形区域中按比例裁剪原始大小的图像。
I tried many ways, 我尝试了很多方法
UIImageView *originalImageView = [[UIImage View alloc] initWithImage:originalImage]];
CGRect rect = CGRectMake(100, 100, 300, 200);
UIImage *cropImage = [UIImage imageWithCGImage:CGImageCreateWithImageInRect([originalImageView.image CGImage], rect)];
But I got the cropImage is just 300*200 sized image, not scale properly. 但是我得到了cropImage只是300 * 200大小的图像,无法正确缩放。
How about doing this, it will preserve the original image quality 这样做如何,它将保留原始图像质量
CGSize bounds = CGSizeMake(320,320) // Considering image is shown in 320*320
CGRect rect = CGRectMake(100, 100, 220, 200); //rectangle area to be cropped
float widthFactor = rect.size.width * (originalImage.size.width/bounds.size.width);
float heightFactor = rect.size.height * (originalImage.size.height/bounds.size.height);
float factorX = rect.origin.x * (originalImage.size.width/bounds.size.width);
float factorY = rect.origin.y * (originalImage.size.height/bounds.size.height);
CGRect factoredRect = CGRectMake(factorX,factorY,widthFactor,heightFactor);
UIImage *cropImage = [UIImage imageWithCGImage:CGImageCreateWithImageInRect([originalImage CGImage], factoredRect)];
And most importantly if you want to crop image that imagePickerController returns, then this can be done by built in function as below, 最重要的是,如果您想裁剪imagePickerController返回的图像,则可以通过以下内置函数来完成,
imagePickerController.allowsEditing = YES;
Why not calculate the scale factor (eg originalImageWidth/smallImageWidth)? 为什么不计算比例因子(例如originalImageWidth / smallImageWidth)? If the rectangle is
(100,100,300,200)
in your small image, you should clip your lage image at size (100*factor,100*factor,300*factor,200*factor)
. 如果您的小图像中的矩形为
(100,100,300,200)
,则应按大小(100*factor,100*factor,300*factor,200*factor)
剪裁图像。
Firstly resize image with size 320*320 using this method: 首先使用以下方法调整尺寸为320 * 320的图像的大小:
+(UIImage *)resizeImage:(UIImage *)image width:(float)width height:(float)height
{
CGSize newSize;
newSize.width = width;
newSize.height = height
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
Now set resized image in imageView 现在在imageView中设置调整大小的图像
UIImage *resizeImage = [YourControllerName resizeImage:originalImage width:320 height:320];
UIImageView *originalImageView = [[UIImage View alloc] initWithImage:resizeImage]];
You can now crop 现在可以裁剪了
CGRect rect = CGRectMake(100, 100, 300, 200);
UIImage *cropImage = [UIImage imageWithCGImage:CGImageCreateWithImageInRect([originalImageView.image CGImage], rect)];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.