How do you crop an image in iOS

后端 未结 4 599
我在风中等你
我在风中等你 2020-12-13 19:05

I have a photo app where you can add stickers in one section. When you\'re finished I want to save the image. Here is the code that I have to do that.

if         


        
相关标签:
4条回答
  • 2020-12-13 19:30

    How about something like this

    CGRect clippedRect  = CGRectMake(self.view.frame.origin.x+91, self.view.frame.origin.y, self.view.frame.size.width-91*2, self.view.frame.size.height-220);
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], clippedRect);
    UIImage *newImage   = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    
    0 讨论(0)
  • 2020-12-13 19:33

    Refer the below link for crop image

    https://github.com/myang-git/iOS-Image-Crop-View

    ** How to Use **
    
    Very easy! It is created to be a drop-in component, so no static library, no extra dependencies. Just copy ImageCropView.h and ImageCropView.m to your project, and implement ImageCropViewControllerDelegate protocol.
    Use it like UIImagePicker:
    - (void)cropImage:(UIImage *)image{
        ImageCropViewController *controller = [[ImageCropViewController alloc] initWithImage:image];
        controller.delegate = self;
        [[self navigationController] pushViewController:controller animated:YES];
    }
    - (void)ImageCropViewController:(ImageCropViewController *)controller didFinishCroppingImage:(UIImage *)croppedImage{
       image = croppedImage;
       imageView.image = croppedImage;
       [[self navigationController] popViewControllerAnimated:YES];
    }
    - (void)ImageCropViewControllerDidCancel:(ImageCropViewController *)controller{
        imageView.image = image;
        [[self navigationController] popViewControllerAnimated:YES];
    }
    
    0 讨论(0)
  • 2020-12-13 19:43

    Following code may help you.

    You should get the correct cropFrame fist by below method

    -(CGRect)cropRectForFrame:(CGRect)frame
    {
        // NSAssert(self.contentMode == UIViewContentModeScaleAspectFit, @"content mode must be aspect fit");
    
        CGFloat widthScale = imageview.superview.bounds.size.width / imageview.image.size.width;
        CGFloat heightScale = imageview.superview.bounds.size.height / imageview.image.size.height;
    
        float x, y, w, h, offset;
        if (widthScale<heightScale) {
            offset = (imageview.superview.bounds.size.height - (imageview.image.size.height*widthScale))/2;
            x = frame.origin.x / widthScale;
            y = (frame.origin.y-offset) / widthScale;
            w = frame.size.width / widthScale;
            h = frame.size.height / widthScale;
        } else {
            offset = (imageview.superview.bounds.size.width - (imageview.image.size.width*heightScale))/2;
            x = (frame.origin.x-offset) / heightScale;
            y = frame.origin.y / heightScale;
            w = frame.size.width / heightScale;
            h = frame.size.height / heightScale;
        }
        return CGRectMake(x, y, w, h);
    }
    

    Then you need to call this method to get cropped image

    - (UIImage *)imageByCropping:(UIImage *)image toRect:(CGRect)rect
    {
        // you need to update scaling factor value if deice is not retina display
        UIGraphicsBeginImageContextWithOptions(rect.size,
                                               /* your view opaque */ NO,
                                               /* scaling factor */ 2.0);
    
        // stick to methods on UIImage so that orientation etc. are automatically
        // dealt with for us
        [image drawAtPoint:CGPointMake(-rect.origin.x, -rect.origin.y)];
    
        UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return result;
    }
    
    0 讨论(0)
  • 2020-12-13 19:44
    - (UIImage*)imageByCropping:(CGRect)rect
    {
        //create a context to do our clipping in
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef currentContext = UIGraphicsGetCurrentContext();
    
        //create a rect with the size we want to crop the image to
        //the X and Y here are zero so we start at the beginning of our
        //newly created context
        CGRect clippedRect = CGRectMake(0, 0, rect.size.width, rect.size.height);
        CGContextClipToRect( currentContext, clippedRect);
    
        //create a rect equivalent to the full size of the image
        //offset the rect by the X and Y we want to start the crop
        //from in order to cut off anything before them
        CGRect drawRect = CGRectMake(rect.origin.x * -1,
                                     rect.origin.y * -1,
                                     self.size.width,
                                     self.size.height);
    
        //draw the image to our clipped context using our offset rect
        // CGContextDrawImage(currentContext, drawRect, self.CGImage);
        [self drawInRect:drawRect]; // This will fix getting inverted image from context.
    
        //pull the image from our cropped context
        UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext();
    
        //pop the context to get back to the default
        UIGraphicsEndImageContext();
    
        //Note: this is autoreleased
        return cropped;
    }
    
    0 讨论(0)
提交回复
热议问题