Skip to content

剪切图的生成大小为何要乘上屏幕的倍数? 我想拿到屏幕剪切的大小图没有配置项可以设置 #1712

Description

@Runyalsj

提bug前必看
请先回答下列三个问题,否则不允处理,谢谢配合。
1、我最新的Demo是否有这个bug?【如果Demo没问题,请升级新版】
答:有

2、你用的是什么版本?升级到最新版后是否正常?
答:3.8.7

3、是否有改动过我库内部的代码?【如有,请说明改动点】
答:无

bug内容描述
剪切图的生成大小跟自己的想要效果有差异 没有配置项可以更改

我如何复现这个bug?
在裁剪的manager当中代码可以查看到

截图
WeChat7d1af2428ba528cbfbfc6401a6bad0c1

Activity

  1. banchichen commented on Feb 6, 2025

    @banchichen
    Owner

    能详细介绍下,目前这种效果,对你的不利影响是?

  2. 0x1306a94 commented on Sep 30, 2025

    @0x1306a94

    @banchichen 原图尺寸小,但经过这么裁剪后。尺寸变大了。

  3. 0x1306a94 commented on Sep 30, 2025

    @0x1306a94

    下面的实现是按照原图尺寸裁剪

    + (UIImage *)cropImageView:(UIImageView *)imageView toRect:(CGRect)rect zoomScale:(double)zoomScale containerView:(UIView *)containerView {
            CGImageRef sourceImageRef = imageView.image.CGImage;
        if (sourceImageRef == NULL) {
            return nil;
        }
        
        /**
         * 视图结构如下
         * containerView
         *     UICollectionView
         *          TZPhotoPreviewCell
         *              contentView
         *                  TZPhotoPreviewView
         *                      UIScrollView
         *                          imageContainerView (scrollview 的 zoomingView)
         *                              imageView
         *      cropView
         *
         *  rect 对应了 cropView.frame
         *
         */
        
        /// imageView 不是UIScrollView 的 zoomingView,所以需要取superview
        CGRect cutRect = [imageView.superview convertRect:rect fromView:containerView];
        CGSize imageSize = imageView.image.size;
        /// 图片与 控件的比例
        CGFloat scale = imageSize.width / CGRectGetWidth(imageView.superview.frame) * zoomScale;
        /// 将剪裁区域坐标转换到图片尺寸上
        cutRect = CGRectMake(cutRect.origin.x * scale, cutRect.origin.y * scale, cutRect.size.width * scale, cutRect.size.height * scale);
        cutRect = CGRectIntersection(cutRect, CGRectMake(0, 0, imageSize.width, imageSize.height));
        if (CGRectIsNull(cutRect) || CGRectIsEmpty(cutRect)) {
            return nil;
        }
        
        CGRect flatCutRect = cutRect;
        flatCutRect.size.width = fmin(ceil(flatCutRect.size.width), imageSize.width);
        flatCutRect.size.height = fmin(ceil(flatCutRect.size.height), imageSize.height);
        if (CGPointEqualToPoint(flatCutRect.origin, CGPointZero) && CGSizeEqualToSize(flatCutRect.size, imageSize)) {
            // 尺寸一致,不执行裁剪处理
            return [[TZImageManager manager] fixOrientation:imageView.image];
        }
        
        CGImageRef cropedImageRef = CGImageCreateWithImageInRect(sourceImageRef, cutRect);
        if (cropedImageRef == NULL) {
            return nil;
        }
        UIImage *cropedImage = [UIImage imageWithCGImage:cropedImageRef];
        cropedImage = [[TZImageManager manager] fixOrientation:cropedImage];
        CGImageRelease(cropedImageRef);
        return cropedImage;
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions