Cropping center square of UIImage

后端 未结 18 604
日久生厌
日久生厌 2020-12-07 23:09

So here\'s where I\'ve made it so far. I am using a UIImage captured from the camera and can crop the center square when in landscape. For some reason this doesn\'t transl

相关标签:
18条回答
  • 2020-12-07 23:15

    improved from @Elmundo's answer

    +(UIImage *)getCenterMaxSquareImageByCroppingImage:(UIImage *)image withOrientation:(UIImageOrientation)imageOrientation
    
    {
        CGSize centerSquareSize;
        double oriImgWid = CGImageGetWidth(image.CGImage);
        double oriImgHgt = CGImageGetHeight(image.CGImage);
        NSLog(@"oriImgWid==[%.1f], oriImgHgt==[%.1f]", oriImgWid, oriImgHgt);
        if(oriImgHgt <= oriImgWid) {
            centerSquareSize.width = oriImgHgt;
            centerSquareSize.height = oriImgHgt;
        }else {
            centerSquareSize.width = oriImgWid;
            centerSquareSize.height = oriImgWid;
        }
    
        NSLog(@"squareWid==[%.1f], squareHgt==[%.1f]", centerSquareSize.width, centerSquareSize.height);
    
        double x = (oriImgWid - centerSquareSize.width) / 2.0;
        double y = (oriImgHgt - centerSquareSize.height) / 2.0;
        NSLog(@"x==[%.1f], x==[%.1f]", x, y);
    
        CGRect cropRect = CGRectMake(x, y, centerSquareSize.height, centerSquareSize.width);
        CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);
    
        UIImage *cropped = [UIImage imageWithCGImage:imageRef scale:0.0 orientation:imageOrientation];
        CGImageRelease(imageRef);
    
    
        return cropped;
    }
    
    0 讨论(0)
  • 2020-12-07 23:17

    Swift 2 using UIImage Extension

    extension UIImage
    {    
        func imageByCroppingImage(size : CGSize) -> UIImage
        {
            let refWidth : CGFloat = CGFloat(CGImageGetWidth(self.CGImage))
            let refHeight : CGFloat = CGFloat(CGImageGetHeight(self.CGImage))
    
            let x = (refWidth - size.width) / 2
            let y = (refHeight - size.height) / 2
    
            let cropRect = CGRectMake(x, y, size.width, size.height)
            let imageRef = CGImageCreateWithImageInRect(self.CGImage, cropRect)
    
            let cropped : UIImage = UIImage(CGImage: imageRef!, scale: 0, orientation: self.imageOrientation)
    
    
            return cropped
        }
    }
    
    0 讨论(0)
  • 2020-12-07 23:19

    Try something like this:

    - (UIImage *)imageByCroppingImage:(UIImage *)image toSize:(CGSize)size
    {
        double x = (image.size.width - size.width) / 2.0;
        double y = (image.size.height - size.height) / 2.0;
    
        CGRect cropRect = CGRectMake(x, y, size.height, size.width);
        CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);
    
        UIImage *cropped = [UIImage imageWithCGImage:imageRef];
        CGImageRelease(imageRef);
    
        return cropped;
    }
    

    As you see, I don't specify orientation in the call to UIImage imageWithCGImage:. I wonder if that's the problem in your code.

    0 讨论(0)
  • 2020-12-07 23:19

    i'm dealing with the following

        extension UIImage {
    
        var cgImageWidth: Int { return cgImage?.width ?? 0 }
        var cgImageheight: Int { return cgImage?.height ?? 0 }
        var blance: CGFloat { return min(size.width, size.height)}
        var blanceSize: CGSize { return CGSize(width: blance, height: blance) }
        var blanceRect: CGRect { return CGRect(origin: .zero, size: blanceSize) }
    
        var roundedImage: UIImage? {
            UIGraphicsBeginImageContextWithOptions(blanceSize, false, scale)
            defer { UIGraphicsEndImageContext() }
            guard let cgImage = cgImage?.cropping(to: CGRect(origin: CGPoint(x: max(0, CGFloat(cgImageWidth) - blance)/2.0, y: max(0, CGFloat(cgImageheight) - blance)/2.0), size: blanceSize)) else { return nil }
            UIBezierPath(ovalIn: blanceRect).addClip()
    
            UIImage(cgImage: cgImage, scale: 1.0, orientation: self.imageOrientation).draw(in: blanceRect)
            return UIGraphicsGetImageFromCurrentImageContext()
        }
    
    }
    
    0 讨论(0)
  • 2020-12-07 23:20

    Based on the checked answer by Elmundo and its swift version by Imbrue, here is the same solution that automatically calculates the size of the image's center (taking orientation into account), and with error consideration:

    func cropImageToSquare(image: UIImage) -> UIImage? {
        var imageHeight = image.size.height
        var imageWidth = image.size.width
    
        if imageHeight > imageWidth {
            imageHeight = imageWidth
        }
        else {
            imageWidth = imageHeight
        }
    
        let size = CGSize(width: imageWidth, height: imageHeight)
    
        let refWidth : CGFloat = CGFloat(CGImageGetWidth(image.CGImage))
        let refHeight : CGFloat = CGFloat(CGImageGetHeight(image.CGImage))
    
        let x = (refWidth - size.width) / 2
        let y = (refHeight - size.height) / 2
    
        let cropRect = CGRectMake(x, y, size.height, size.width)
        if let imageRef = CGImageCreateWithImageInRect(image.CGImage, cropRect) {
            return UIImage(CGImage: imageRef, scale: 0, orientation: image.imageOrientation)
        }
    
       return nil
    }
    

    Swift 3 version

    func cropImageToSquare(image: UIImage) -> UIImage? {
        var imageHeight = image.size.height
        var imageWidth = image.size.width
    
        if imageHeight > imageWidth {
            imageHeight = imageWidth
        }
        else {
            imageWidth = imageHeight
        }
    
        let size = CGSize(width: imageWidth, height: imageHeight)
    
        let refWidth : CGFloat = CGFloat(image.cgImage!.width)
        let refHeight : CGFloat = CGFloat(image.cgImage!.height)
    
        let x = (refWidth - size.width) / 2
        let y = (refHeight - size.height) / 2
    
        let cropRect = CGRect(x: x, y: y, width: size.height, height: size.width)
        if let imageRef = image.cgImage!.cropping(to: cropRect) {
            return UIImage(cgImage: imageRef, scale: 0, orientation: image.imageOrientation)
        }
    
        return nil
    }
    
    0 讨论(0)
  • 2020-12-07 23:23

    what about getting help from UIImageView

    + (UIImage *)laodSquareImage:(UIImage *)image withDiameter:(CGFloat)diameter  
    {
        CGRect frame = CGRectMake(0.0f, 0.0f, diameter, diameter);
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = YES;
        imageView.image = image;
        CALayer *layer = imageView.layer;
    
    
        layer.masksToBounds = YES;
        UIGraphicsBeginImageContextWithOptions(imageView.bounds.size,NO, [UIScreen mainScreen].scale);
        CGContextRef context = UIGraphicsGetCurrentContext();
        [layer renderInContext:context];
    
    
        UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return roundedImage;
    }
    

    And you can also make the image circle by adding just a line.

    layer.cornerRadius =MAX( imageView.frame.size.height,imageView.frame.size.width)/2;
    
    0 讨论(0)
提交回复
热议问题