【问题标题】:How do you crop an image in iOS如何在 iOS 中裁剪图像
【发布时间】:2012-04-22 05:01:23
【问题描述】:

我有一个照片应用程序,您可以在其中在一个部分中添加贴纸。完成后,我想保存图像。这是我必须这样做的代码。

if(UIGraphicsBeginImageContextWithOptions != NULL)
{
    UIGraphicsBeginImageContextWithOptions(self.view.frame.size, YES, 2.5);
} else {
    UIGraphicsBeginImageContext(self.view.frame.size);
}
CGContextRef contextNew=UIGraphicsGetCurrentContext();

[self.view.layer renderInContext:contextNew];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

现在保存的图像是图像的全屏,这很好,但现在我需要裁剪图像,我不知道如何。您可以在下面的链接中看到图片: http://dl.dropbox.com/u/19130454/Photo%202012-04-09%201%2036%2018%20PM.png

我需要裁剪: 左右91px 距离底部 220 像素

任何帮助将不胜感激。如果我没有解释清楚,请告诉我,我会尽力重新解释。

【问题讨论】:

标签: iphone ios xcode ios4


【解决方案1】:

这样的事情怎么样

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);

【讨论】:

  • 这会忽略图像方向,因为 UIImage 支持但 CGImage 不支持
【解决方案2】:

以下代码可能会对您有所帮助。

你应该通过下面的方法得到正确的cropFrame拳头

-(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);
}

那么你需要调用这个方法来获取裁剪后的图像

- (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;
}

【讨论】:

    【解决方案3】:
    - (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;
    }
    

    【讨论】:

    • 什么是self.CGImage?
    • 我使用这种方法可以按预期正确裁剪图像。但是它自动反转了。你能帮我吗?谢谢。
    • @user2534373 尝试删除这个 UIGraphicsEndImageContext();
    • self.CGImage 的意思是 image.CGImage
    • 尝试添加这个来补偿垂直翻转:CGContextScaleCTM(currentContext, 1.0, -1.0);
    【解决方案4】:

    请参阅以下链接以获取裁剪图像

    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];
    }
    

    【讨论】:

    • 请从链接中添加一些内容。
    • 嗨,我的自我 ramani 一个帮助.. 我的项目裁剪图像,如面部形状图像,还有许多不同类型的形状裁剪和裁剪视图移动和缩放目标 c
    猜你喜欢
    • 2016-06-16
    • 2011-10-28
    • 2013-05-25
    • 2015-02-02
    • 2012-03-20
    • 2012-08-10
    • 1970-01-01
    • 1970-01-01
    • 2012-12-12
    相关资源
    最近更新 更多