2016-05-30 43 views
0

我正在使用下面的裁剪方法來裁剪Uiimage坐在UIImageView,然後坐在UIScrollView。問題cropImage方法在UIScrollView中的圖像產生錯誤的圖像

-(UIImage *)cropImage:(UIImage *)image 
{ 
    float scale = 1.0f/_scrollView.zoomScale; 

    NSLog(@"Oh and heres that zoomScale: %f", _scrollView.zoomScale); 

    CGRect visibleRect; 
    visibleRect.origin.x = _scrollView.contentOffset.x * scale; 
    visibleRect.origin.y = _scrollView.contentOffset.y * scale; 
    visibleRect.size.width = _scrollView.bounds.size.width * scale; 
    visibleRect.size.height = _scrollView.bounds.size.height * scale; 

    NSLog(@"Oh and here's that CGRect: %f", visibleRect.origin.x); 
    NSLog(@"Oh and here's that CGRect: %f", visibleRect.origin.y); 
    NSLog(@"Oh and here's that CGRect: %f", visibleRect.size.width); 
    NSLog(@"Oh and here's that CGRect: %f", visibleRect.size.height); 

    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], visibleRect); 
    UIImage *croppedImage = [[UIImage alloc] initWithCGImage:imageRef]; 
    CGImageRelease(imageRef); 

    return croppedImage; 
} 

我需要將圖像裁剪爲CGSize(321,115)。在剪切圖像並看到打印結果後,我可以看到visibleRect是(0,0,321,115) - 它應該是這樣的,然後UIImage的寬度爲321,高度爲115。由於某些原因,圖像似乎完全放大得太遠了(該方法將原始圖像的一小部分裁剪爲321x115的大小)。

爲什麼此方法不能正確裁剪我的圖像?

- 作爲一個方面說明:當我調用這個方法時,我打電話給_croppedImage = [self cropImage:_imageView.image];,它將自定義UIView類的UIImage屬性設置爲裁剪後的圖像。

回答

2

請嘗試此功能。它可能會幫助你。

參數:

  1. UIImage
  2. CGSize (321,115)或任何尺寸

//裁剪圖像 - 圖像就會突然從完整圖像

- (UIImage *)cropImageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize { 
    double ratio; 
    double delta; 
    CGPoint offset; 

    //make a new square size, that is the resized imaged width 
    CGSize sz = CGSizeMake(newSize.width, newSize.width); 

    //figure out if the picture is landscape or portrait, then 
    //calculate scale factor and offset 
    if (image.size.width > image.size.height) { 
     ratio = newSize.width/image.size.width; 
     delta = (ratio*image.size.width - ratio*image.size.height); 
     offset = CGPointMake(delta/2, 0); 
    } 
    else { 
     ratio = newSize.width/image.size.height; 
     delta = (ratio*image.size.height - ratio*image.size.width); 
     offset = CGPointMake(0, delta/2); 
    } 

    //make the final clipping rect based on the calculated values 
    CGRect clipRect = CGRectMake(-offset.x, 
           -offset.y, 
           (ratio * image.size.width) + delta, 
           (ratio * image.size.height) + delta); 


    //start a new context, with scale factor 0.0 so retina displays get 
    //high quality image 
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { 
     UIGraphicsBeginImageContextWithOptions(sz, YES, 0.0); 
    } else { 
     UIGraphicsBeginImageContext(sz); 
    } 
    UIRectClip(clipRect); 
    [image drawInRect:clipRect]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return newImage; 
} 

要裁剪的僅選定部分圖片

請檢查this link

+0

這段代碼如何考慮我的UIScrollView zoomScale?我必須考慮UIScrollView的scaleFactor,否則這不會裁剪所選區域 – Chisx

+0

這意味着您只想裁剪選定的區域。對 ?如果是,請檢查已編輯的答案 –

+0

是的,我幾乎工作使用http://stackoverflow.com/questions/7760191/cropping-a-uiimage-according-to-the-viewport-inside-a-uiscrollview但有問題與該代碼..只有稍微關閉。是的,只有選定的區域..基本上是縮放區域或出現在UIScrollView中的區域。這很接近,但那不是我正在尋找的內容,我在這裏分享的鏈接基本上是我在尋找的內容,但它不能完全正常工作。 – Chisx