从 iPhone 的照片库中调整方形裁剪图像的正确方法而不会失真

Posted

技术标签:

【中文标题】从 iPhone 的照片库中调整方形裁剪图像的正确方法而不会失真【英文标题】:Proper way to resize square cropped images from iPhone's photo library without distortion 【发布时间】:2014-07-04 22:07:01 【问题描述】:

我想从照片库上传一些方形裁剪图像,但上传的图像的纵横比会以某种方式损坏。我不明白为什么,因为我将它裁剪为 440 x 440,所以比例应该相同。

如果有人可以向我展示如何将图像裁剪为方形并调整大小而不会造成任何变形和质量损失,我将非常高兴。我只需要使用设备照片库中的图像即可。

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex


    else if (buttonIndex == 1)
    
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary] == YES) 

            UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
            imagePicker.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;

            imagePicker.delegate = self;

            imagePicker.allowsEditing = YES;
            [self presentViewController:imagePicker animated:YES completion:nil];
            [self.tableView reloadData];
        
    


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info


    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

    [picker dismissViewControllerAnimated:YES completion:nil];

    // resize image
    UIGraphicsBeginImageContext(CGSizeMake(440, 440));
    [image drawInRect: CGRectMake(0, 0, 440, 440)];
    UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    NSData *imageData = UIImageJPEGRepresentation(smallImage, 1.0);
    [self uploadImage:imageData];

【问题讨论】:

您没有使用drawInRect 裁剪它们。这可能就是你想要的:***.com/questions/14203951/… @vv88,你试过我的答案了吗? 【参考方案1】:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

    [picker dismissViewControllerAnimated:YES completion:nil];

    // Fixing orientation and transform of picked image
    image = [image fixOrientation];

    // Crop the image
    UIImage *smallImage = [image cropToRect:CGRectMake(0, 0, 440, 440)];

    NSData *imageData = UIImageJPEGRepresentation(smallImage, 1.0);
    [self uploadImage:imageData];

使用以下方法在 UIImage 上创建一个类别

@implementation UIImage (FixOrientation)

- (UIImage *)cropToRect:(CGRect)rect 
    rect.size.height = rect.size.height * [self scale];
    rect.size.width = rect.size.width * [self scale];
    rect.origin.x = rect.origin.x * [self scale];
    rect.origin.y = rect.origin.y * [self scale];
    CGImageRef sourceImageRef = [self CGImage];
    CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef scale:[self scale] orientation:[self imageOrientation]];
    CGImageRelease(newImageRef);

    return newImage;


- (UIImage *)fixOrientation
        
    // No-op if the orientation is already correct
    if (self.imageOrientation == UIImageOrientationUp) return self;

    // We need to calculate the proper transformation to make the image upright.
    // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
    CGAffineTransform transform = CGAffineTransformIdentity;

    switch (self.imageOrientation) 
        case UIImageOrientationDown:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;

        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
            transform = CGAffineTransformTranslate(transform, self.size.width, 0);
            transform = CGAffineTransformRotate(transform, M_PI_2);
            break;

        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, 0, self.size.height);
            transform = CGAffineTransformRotate(transform, -M_PI_2);
            break;
        case UIImageOrientationUp:
        case UIImageOrientationUpMirrored:
            break;
    

    switch (self.imageOrientation) 
        case UIImageOrientationUpMirrored:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, self.size.width, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;

        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, self.size.height, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
        case UIImageOrientationUp:
        case UIImageOrientationDown:
        case UIImageOrientationLeft:
        case UIImageOrientationRight:
            break;
    

    // Now we draw the underlying CGImage into a new context, applying the transform
    // calculated above.
    CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height,
                                             CGImageGetBitsPerComponent(self.CGImage), 0,
                                             CGImageGetColorSpace(self.CGImage),
                                             CGImageGetBitmapInfo(self.CGImage));
    CGContextConcatCTM(ctx, transform);
    switch (self.imageOrientation) 
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            // Grr...
            CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage);
            break;

        default:
            CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage);
            break;
    

    // And now we just create a new UIImage from the drawing context
    CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
    UIImage *img = [UIImage imageWithCGImage:cgimg];
    CGContextRelease(ctx);
    CGImageRelease(cgimg);
    return img;


@end

【讨论】:

请在您尝试此操作时告诉我结果。【参考方案2】:

drawInRect: 正在强制图像更改其外观以适应正方形区域。您需要计算一个覆盖正方形区域的原始纵横比的区域。

来自UIImage category 的代码:

- (UIImage *)imageCroppedToFill:(CGSize)size

    CGFloat factor = MAX(size.width / self.size.width,
                         size.height / self.size.height);

    UIGraphicsBeginImageContextWithOptions(size,
                                           YES,                     // Opaque
                                           self.scale);             // Use image scale

    CGRect rect = CGRectMake((size.width - nearbyintf(self.size.width * factor)) / 2.0,
                             (size.height - nearbyintf(self.size.height * factor)) / 2.0,
                             nearbyintf(self.size.width * factor),
                             nearbyintf(self.size.height * factor));
    [self drawInRect:rect];
    UIImage * croppedImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    NBULogInfo(@"Image %@ %@ cropped to %@ %@",
               self,
               NSStringFromCGSize(self.size),
               croppedImage,
               NSStringFromCGSize(croppedImage.size));

    return croppedImage;

【讨论】:

【参考方案3】:

您可以使用 CoreGraphics 动态绘制蒙版。看看这个,希望对你有帮助;

https://***.com/a/9585892/2622013

【讨论】:

以上是关于从 iPhone 的照片库中调整方形裁剪图像的正确方法而不会失真的主要内容,如果未能解决你的问题,请参考以下文章

如何使用(非方形)曲奇模板裁剪照片?

如何方形裁剪 Flutter 相机预览

响应式图像:调整大小或动态裁剪?

如何在 iPhone 中以高质量调整图像大小

在 PHP 中将图像裁剪为正方形,而不是调整为相同的纵横比

swift SWIFT:将图像裁剪为方形(并调整大小)