gpt4 book ai didi

iphone - CGImageCreateWithImageInRect 没有正确裁剪

转载 作者:可可西里 更新时间:2023-11-01 05:38:48 26 4
gpt4 key购买 nike

我在使用 CGImageCreateWithImageInRect 裁剪照片时遇到了麻烦。我的应用程序让用户移动/放大图像直到它填满 316 x 316 View ,然后我希望它裁剪框外的任何区域并将图像保存为 UIImage。我通过取 x 和 y ImageView 原点的绝对值来确定裁剪原点,因为这使我回到包含 ImageView 的 View /框的 0,0。下面是代码:

    CGRect croppedRect = CGRectMake(fabs(widthDistanceToOrigin), fabs(HeightDistanceToOrigin), 316, 316);
CGImageRef croppedImageRef = CGImageCreateWithImageInRect(image.CGImage, croppedRect);
UIImage *croppedImage = [UIImage imageWithCGImage: croppedImageRef scale: 1 / (imageScale) orientation: image.imageOrientation];
CGImageRelease(croppedImageRef);

这快把我逼疯了,我无法让它裁剪我想要的区域。我已经让它正确缩放,但问题似乎出在裁剪矩形的 x 和 y 原点上,它与它应该占用的区域有很大的偏差(有时我会得到奇怪的结果)。

此外,似乎对于用手机相机拍摄的图像,我必须将裁剪矩形中的所有内容乘以 2 才能使其大小正确。很奇怪。

所以我想知道,有没有人知道如何解决这个问题,或者有没有人有更好的裁剪方法(请记住,我需要裁剪照片中的一个区域)。谢谢!

最佳答案

关于这个主题的帖子太多了,而且所有的帖子都有完全相同的答案,利用:CGImageCreateWithImageInRect但它们都没有完全解决试图裁剪的内容定位错误的问题。

然而,只有一个线程(埋藏在深处)有一个其他人都遗漏的小细节...... How to crop a UIImageView to a new UIImage in 'aspect fit' mode?

进入的'rect'参数CGImageCreateWithImageInRect(图像。CGImage,croppedRect)需要表示(取自)UIImage,而不是 UIImageView。

这应该可以解决在坐标系上定位错误的问题。

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:   (NSDictionary *)info
{
oImage = [info valueForKey:UIImagePickerControllerOriginalImage];
NSLog(@"Original Image size width: %f x height: %f", oImage.size.width, oImage.size.height);


// crop image according to what the image picker view is displaying
CGRect rect = CGRectMake(0, 40, 960.0f, 960.0f); // note: 960 x 1280 is what natively comes from capturing and image
nImage = [BGViewModifiers cropImage:oImage inRect:rect];

// scale image down for display and creating kombie
CGSize size = CGSizeMake(320.0f, 320.0f);
nImage = [BGViewModifiers imageFromImage:nImage scaledToSize:size];

NSLog(@"New Image size width: %f x height: %f", [nImage size].width, [nImage size].height);
}

//ref: http://stackoverflow.com/a/25293588/2298002
+ (UIImage *)cropImage:(UIImage*)image inRect:(CGRect)rect
{
double (^rad)(double) = ^(double deg) {
return deg / 180.0 * M_PI;
};

CGAffineTransform rectTransform;
switch (image.imageOrientation) {
case UIImageOrientationLeft:
rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(90)), 0, -image.size.height);
break;
case UIImageOrientationRight:
rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(-90)), -image.size.width, 0);
break;
case UIImageOrientationDown:
rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(-180)), -image.size.width, -image.size.height);
break;
default:
rectTransform = CGAffineTransformIdentity;
};
rectTransform = CGAffineTransformScale(rectTransform, image.scale, image.scale);

CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], CGRectApplyAffineTransform(rect, rectTransform));
UIImage *result = [UIImage imageWithCGImage:imageRef scale:image.scale orientation:image.imageOrientation];
CGImageRelease(imageRef);

return result;
}

+ (UIImage*)imageFromImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return newImage;
}

关于iphone - CGImageCreateWithImageInRect 没有正确裁剪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8061412/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com