2012-04-09 81 views
25

我有一個照片應用程序,您可以在一個部分添加貼紙。完成後,我想保存圖像。這是我必須這樣做的代碼。如何在iOS中裁剪圖像

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並從底部

任何幫助,將不勝感激權 220px。如果我沒有解釋清楚,請讓我知道,我會盡我所能重新解釋。

+1

這可能是你的好來源:開源庫快速將圖像裁剪添加到iOS應用程序中](http://maniacdev.com/2011/10/open-source-library-to-add-image-cropping-into-an-ios-app-quickly/) – Lucien 2012-04-09 21:33:50

+0

可能重複[圖像裁剪API爲iOS](http://stackoverflow.com/questions/7087435/image-cropping-api-for-ios) – NAlexN 2015-04-08 15:16:55

回答

34

如何像這樣

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

非常感謝您的幫助。那完美的工作! – flite3 2012-04-10 02:26:14

+3

這忽略了圖像的方向,因爲UIImage支持它,但CGImage沒有 – Dustin 2014-08-13 20:31:44

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

什麼是self.CGImage? – Dvole 2014-06-17 18:30:33

+1

我使用這種方法我的圖像按預期正確裁剪。但它自動倒轉。你能幫我解答嗎? – iHulk 2014-06-19 12:31:20

+0

@ user2534373嘗試刪除這個 UIGraphicsEndImageContext(); – Mohit 2014-06-19 13:38:02

-1

請參考下面的鏈接,剪切圖像

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

請從鏈接添加一些內容。 – Robert 2015-09-15 13:11:45

+0

你好我的自我ramani one幫助..我的項目作物圖像像臉形狀圖像也許多不同類型形狀作物和作物視圖移動和縮放目標c – 2017-10-04 05:02:41

+0

使用此演示圖像作物https://github.com/gavinbunney/Toucan – 2017-10-04 10:35:53

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