2016-05-30 45 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