Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

createFrameAtIndex has a bug #3351

Closed
AKZHW opened this issue Jun 6, 2022 · 5 comments · Fixed by #3354
Closed

createFrameAtIndex has a bug #3351

AKZHW opened this issue Jun 6, 2022 · 5 comments · Fixed by #3354
Labels

Comments

@AKZHW
Copy link

AKZHW commented Jun 6, 2022

 "  decodingOptions[(__bridge NSString *)kCGImageSourceCreateThumbnailWithTransform] = @(preserveAspectRatio);
    CGFloat maxPixelSize;
    if (preserveAspectRatio) {
        CGFloat pixelRatio = pixelWidth / pixelHeight ;
        CGFloat thumbnailRatio = thumbnailSize.width / thumbnailSize.height;
        if (pixelRatio > thumbnailRatio) {
            maxPixelSize = thumbnailSize.width;
        } else {
            maxPixelSize = thumbnailSize.height;
        }
    } else {
        maxPixelSize = MAX(thumbnailSize.width, thumbnailSize.height);
    }
    decodingOptions[(__bridge NSString *)kCGImageSourceThumbnailMaxPixelSize] = @(maxPixelSize);
    decodingOptions[(__bridge NSString *)kCGImageSourceCreateThumbnailFromImageAlways] = @(YES);
    imageRef = CGImageSourceCreateThumbnailAtIndex(source, index, (__bridge CFDictionaryRef)[decodingOptions copy]);"

There is a precision loss problem in this line ” CGFloat pixelRatio = pixelWidth / pixelHeight ; “

then "

        if (pixelRatio > thumbnailRatio) {
            maxPixelSize = thumbnailSize.width;
        } else {
            maxPixelSize = thumbnailSize.height;
        }"

should after Calculate the width and height in the same proportion, and then calculate maxsize

@dreampiggy
Copy link
Contributor

dreampiggy commented Jun 7, 2022

Maybe #3262 already solve this.
Check v5.12.0+ again ?

@AKZHW
Copy link
Author

AKZHW commented Jun 8, 2022

only fix precision loss problem, but calculate maxPixelSize has a problem,

image

I think should use this code
CGFloat maxPixelSize;
if (preserveAspectRatio) {
CGFloat pixelRatio = pixelWidth / pixelHeight;
CGFloat thumbnailRatio = thumbnailSize.width / thumbnailSize.height;
if (pixelRatio > thumbnailRatio) {
maxPixelSize = MAX(thumbnailSize.width, thumbnailSize.width / pixelRatio);
} else {
maxPixelSize = MAX(thumbnailSize.height * pixelRatio, thumbnailSize.height);
}
} else {
maxPixelSize = MAX(thumbnailSize.width, thumbnailSize.height);
}

@AKZHW
Copy link
Author

AKZHW commented Jun 8, 2022

for example my picture origin size 360 * 136 , request size 200 * 80, the result should get 200 * 75, bug use you code get 80 * 30

@dreampiggy dreampiggy added the bug label Jun 12, 2022
@dreampiggy
Copy link
Contributor

I think you're right.

The documented design of that preserveAspectRatio should use the latest near but not larger value of one edge. So current these code:

            if (pixelRatio > thumbnailRatio) {
                maxPixelSize = thumbnailSize.width;
            } else {
                maxPixelSize = thumbnailSize.height;
            }

will only assume to use another edge's length as input thumbnail pixel width. Which will make the final thumbnail smaller than you expected.

@dreampiggy
Copy link
Contributor

Fixed in 5.12.6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants