Cropping an UIImage

后端 未结 24 1815
轮回少年
轮回少年 2020-11-22 02:45

I\'ve got some code that resizes an image so I can get a scaled chunk of the center of the image - I use this to take a UIImage and return a small, square repre

相关标签:
24条回答
  • 2020-11-22 03:20

    Swift 5:

    extension UIImage {
        func cropped(rect: CGRect) -> UIImage? {
            guard let cgImage = cgImage else { return nil }
    
            UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
            let context = UIGraphicsGetCurrentContext()
    
            context?.translateBy(x: 0.0, y: self.size.height)
            context?.scaleBy(x: 1.0, y: -1.0)
            context?.draw(cgImage, in: CGRect(x: rect.minX, y: rect.minY, width: self.size.width, height: self.size.height), byTiling: false)
    
    
            let croppedImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
    
            return croppedImage
        }
    }
    
    0 讨论(0)
  • 2020-11-22 03:22

    You can make a UIImage category and use it wherever you need. Based on HitScans response and comments bellow it.

    @implementation UIImage (Crop)
    
    - (UIImage *)crop:(CGRect)rect {
    
        rect = CGRectMake(rect.origin.x*self.scale, 
                          rect.origin.y*self.scale, 
                          rect.size.width*self.scale, 
                          rect.size.height*self.scale);       
    
        CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], rect);
        UIImage *result = [UIImage imageWithCGImage:imageRef 
                                              scale:self.scale 
                                        orientation:self.imageOrientation]; 
        CGImageRelease(imageRef);
        return result;
    }
    
    @end
    

    You can use it this way:

    UIImage *imageToCrop = <yourImageToCrop>;
    CGRect cropRect = <areaYouWantToCrop>;   
    
    //for example
    //CGRectMake(0, 40, 320, 100);
    
    UIImage *croppedImage = [imageToCrop crop:cropRect];
    
    0 讨论(0)
  • 2020-11-22 03:22

    Below code snippet might help.

    import UIKit
    
    extension UIImage {
        func cropImage(toRect rect: CGRect) -> UIImage? {
            if let imageRef = self.cgImage?.cropping(to: rect) {
                return UIImage(cgImage: imageRef)
            }
            return nil
        }
    }
    
    0 讨论(0)
  • 2020-11-22 03:22

    Looks a little bit strange but works great and takes into consideration image orientation:

    var image:UIImage = ...
    
    let img = CIImage(image: image)!.imageByCroppingToRect(rect)
    image = UIImage(CIImage: img, scale: 1, orientation: image.imageOrientation)
    
    0 讨论(0)
  • 2020-11-22 03:24

    None of the answers here handle all of the scale and rotation issues 100% correctly. Here's a synthesis of everything said so far, up-to-date as of iOS7/8. It's meant to be included as a method in a category on UIImage.

    - (UIImage *)croppedImageInRect:(CGRect)rect
    {
        double (^rad)(double) = ^(double deg) {
            return deg / 180.0 * M_PI;
        };
    
        CGAffineTransform rectTransform;
        switch (self.imageOrientation) {
            case UIImageOrientationLeft:
                rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(90)), 0, -self.size.height);
                break;
            case UIImageOrientationRight:
                rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(-90)), -self.size.width, 0);
                break;
            case UIImageOrientationDown:
                rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(-180)), -self.size.width, -self.size.height);
                break;
            default:
                rectTransform = CGAffineTransformIdentity;
        };
        rectTransform = CGAffineTransformScale(rectTransform, self.scale, self.scale);
    
        CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], CGRectApplyAffineTransform(rect, rectTransform));
        UIImage *result = [UIImage imageWithCGImage:imageRef scale:self.scale orientation:self.imageOrientation];
        CGImageRelease(imageRef);
    
        return result;
    }
    
    0 讨论(0)
  • 2020-11-22 03:26

    Follow Answer of @Arne. I Just fixing to Category function. put it in Category of UIImage.

    -(UIImage*)cropImage:(CGRect)rect{
    
        UIGraphicsBeginImageContextWithOptions(rect.size, false, [self scale]);
        [self drawAtPoint:CGPointMake(-rect.origin.x, -rect.origin.y)];
        UIImage* cropped_image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return cropped_image;
    }
    
    0 讨论(0)
提交回复
热议问题