Iphone 如何在iOS中裁剪图像

Iphone 如何在iOS中裁剪图像,iphone,ios,xcode,ios4,Iphone,Ios,Xcode,Ios4,我有一个照片应用程序,你可以在一个部分添加贴纸。完成后,我想保存图像。这是我必须执行的代码 if(UIGraphicsBeginImageContextWithOptions != NULL) { UIGraphicsBeginImageContextWithOptions(self.view.frame.size, YES, 2.5); } else { UIGraphicsBeginImageContext(self.view.frame.size); } CGContext

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

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();
现在保存的图像是图像的全屏,这很好,但是现在我需要裁剪图像,我不知道如何裁剪。您可以在下面的链接中看到该图像:

我需要裁剪: 左、右各91px 底部220像素


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

像这样的事情怎么样

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

有关裁剪图像,请参阅以下链接


下面的代码可能对您有所帮助

你应该通过下面的方法得到正确的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);
}

这可能是一个很好的来源:self.CGImage的可能副本?我使用此方法,我的图像按预期正确剪裁。但它是自动倒过来的。谢谢。@user2534373尝试删除此UIGraphicsSendImageContext();self.CGImage意味着image.CGImage尝试添加此项以补偿垂直翻转:
CGContextScaleCTM(currentContext,1.0,-1.0)这忽略图像方向,因为UIImage支持它,但CGImage不支持。请从链接添加一些内容。嗨,我的自我ramani one help。。我的项目裁剪图像,如面部形状图像,也有许多不同类型的形状裁剪和裁剪视图移动和缩放目标c
** 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];
}
-(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;
}