【问题标题】:issue with cropImage method on image in UIScrollView yields wrong imageUIScrollView 中图像上的cropImage 方法的问题会产生错误的图像
【发布时间】:2016-05-30 04:14:33
【问题描述】:

我正在使用以下裁剪方法来裁剪位于 UIImageView 中的 uiimage,然后该 UIImageView 位于 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;
}

我需要将图像裁剪为 (321,115) 的 CGSize。在裁剪图像并查看打印结果后,我可以看到 visibleRect 是 (0,0,321,115) - 它应该是什么,croppedImage UIImage 然后具有宽度:321 和高度:115。但是由于某种原因,图像似乎完全放大得太远了(该方法将原始图像的一小部分裁剪为 321x115 的大小)。

为什么这种方法不能正确裁剪我的图像?

-附带说明:当我调用此方法时,我会像这样调用_croppedImage = [self cropImage:_imageView.image];,它将自定义 UIView 类的 UIImage 属性设置为裁剪后的图像。

【问题讨论】:

    标签: ios uiscrollview uiimage


    【解决方案1】:

    请试试这个功能。它可能会帮助你。

    参数:

    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

    【讨论】:

    • 这段代码如何考虑我的 UIScrollView zoomScale?我必须考虑 UIScrollView 的 scaleFactor,否则这不是裁剪所选区域
    • 这意味着您只想裁剪选定的区域。对 ?如果是,请检查编辑后的答案
    • 是的,我几乎可以使用stackoverflow.com/questions/7760191/… 工作,但该代码存在问题.. 只是略有偏差。是的..只有选定的区域..基本上是缩放区域或出现在 UIScrollView 中的区域。这很接近,但这也不是我想要的,我在这里分享的链接基本上就是我想要的,但它并不能完全正常工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-09
    • 2018-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多