Cropping an UIImage

后端 未结 24 1732
轮回少年
轮回少年 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:29

    Here is my UIImage crop implementation which obeys the imageOrientation property. All orientations were thoroughly tested.

    inline double rad(double deg)
    {
        return deg / 180.0 * M_PI;
    }
    
    UIImage* UIImageCrop(UIImage* img, CGRect rect)
    {
        CGAffineTransform rectTransform;
        switch (img.imageOrientation)
        {
            case UIImageOrientationLeft:
                rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(90)), 0, -img.size.height);
                break;
            case UIImageOrientationRight:
                rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(-90)), -img.size.width, 0);
                break;
            case UIImageOrientationDown:
                rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(-180)), -img.size.width, -img.size.height);
                break;
            default:
                rectTransform = CGAffineTransformIdentity;
        };
        rectTransform = CGAffineTransformScale(rectTransform, img.scale, img.scale);
    
        CGImageRef imageRef = CGImageCreateWithImageInRect([img CGImage], CGRectApplyAffineTransform(rect, rectTransform));
        UIImage *result = [UIImage imageWithCGImage:imageRef scale:img.scale orientation:img.imageOrientation];
        CGImageRelease(imageRef);
        return result;
    }
    
    0 讨论(0)
  • 2020-11-22 03:31
    - (UIImage *)getSubImage:(CGRect) rect{
        CGImageRef subImageRef = CGImageCreateWithImageInRect(self.CGImage, rect);
        CGRect smallBounds = CGRectMake(rect.origin.x, rect.origin.y, CGImageGetWidth(subImageRef), CGImageGetHeight(subImageRef));
    
        UIGraphicsBeginImageContext(smallBounds.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextDrawImage(context, smallBounds, subImageRef);
        UIImage* smallImg = [UIImage imageWithCGImage:subImageRef];
        UIGraphicsEndImageContext();
    
        return smallImg;
    }
    
    0 讨论(0)
  • 2020-11-22 03:31
     (UIImage *)squareImageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
        double ratio;
        double delta;
        CGPoint offset;
    
        //make a new square size, that is the resized imaged width
        CGSize sz = CGSizeMake(newSize.width, newSize.width);
    
        //figure out if the picture is landscape or portrait, then
        //calculate scale factor and offset
        if (image.size.width > image.size.height) {
            ratio = newSize.width / image.size.width;
            delta = (ratio*image.size.width - ratio*image.size.height);
            offset = CGPointMake(delta/2, 0);
        } else {
            ratio = newSize.width / image.size.height;
            delta = (ratio*image.size.height - ratio*image.size.width);
            offset = CGPointMake(0, delta/2);
        }
    
        //make the final clipping rect based on the calculated values
        CGRect clipRect = CGRectMake(-offset.x, -offset.y,
                                     (ratio * image.size.width) + delta,
                                     (ratio * image.size.height) + delta);
    
    
        //start a new context, with scale factor 0.0 so retina displays get
        //high quality image
        if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
            UIGraphicsBeginImageContextWithOptions(sz, YES, 0.0);
        } else {
            UIGraphicsBeginImageContext(sz);
        }
        UIRectClip(clipRect);
        [image drawInRect:clipRect];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return newImage;
    }
    
    0 讨论(0)
  • 2020-11-22 03:32

    To crop retina images while keeping the same scale and orientation, use the following method in a UIImage category (iOS 4.0 and above):

    - (UIImage *)crop:(CGRect)rect {
        if (self.scale > 1.0f) {
            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;
    }
    
    0 讨论(0)
  • 2020-11-22 03:34

    Swift version of awolf's answer, which worked for me:

    public extension UIImage {
        func croppedImage(inRect rect: CGRect) -> UIImage {
            let rad: (Double) -> CGFloat = { deg in
                return CGFloat(deg / 180.0 * .pi)
            }
            var rectTransform: CGAffineTransform
            switch imageOrientation {
            case .left:
                let rotation = CGAffineTransform(rotationAngle: rad(90))
                rectTransform = rotation.translatedBy(x: 0, y: -size.height)
            case .right:
                let rotation = CGAffineTransform(rotationAngle: rad(-90))
                rectTransform = rotation.translatedBy(x: -size.width, y: 0)
            case .down:
                let rotation = CGAffineTransform(rotationAngle: rad(-180))
                rectTransform = rotation.translatedBy(x: -size.width, y: -size.height)
            default:
                rectTransform = .identity
            }
            rectTransform = rectTransform.scaledBy(x: scale, y: scale)
            let transformedRect = rect.applying(rectTransform)
            let imageRef = cgImage!.cropping(to: transformedRect)!
            let result = UIImage(cgImage: imageRef, scale: scale, orientation: imageOrientation)
            return result
        }
    }
    
    0 讨论(0)
  • 2020-11-22 03:35

    Swift Extension

    extension UIImage {
        func crop(var rect: CGRect) -> UIImage {
            rect.origin.x*=self.scale
            rect.origin.y*=self.scale
            rect.size.width*=self.scale
            rect.size.height*=self.scale
    
            let imageRef = CGImageCreateWithImageInRect(self.CGImage, rect)
            let image = UIImage(CGImage: imageRef, scale: self.scale, orientation: self.imageOrientation)!
            return image
        }
    }
    
    0 讨论(0)
提交回复
热议问题