0

人,我需要你的帮助。

需要在流上优化缩放图像。我尝试了两种使用保存比例缩放图像的方法,请检查此代码并说,如果您愿意/可以,我该如何优化:

// Draw image in rect (PNG 1000ms, JPG 834ms for 8 pictures)
+ (UIImage *)imageWithImage:(UIImage *)sourceImage
     scaledToSizeWithSameAspectRatio:(CGSize)targetSize
            withInterpolationQuality:(CGInterpolationQuality)quality
{
    CGSize imageSize = sourceImage.size;

    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;

    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;

    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;

    CGPoint thumbnailPoint = (CGPoint){0.0, 0.0};

    if (CGSizeEqualToSize(imageSize, targetSize) == NO) {

        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;

        if (widthFactor > heightFactor) {

            scaledWidth  = width * widthFactor;
            scaledHeight = height * widthFactor;

            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
        }
        else {

            scaledWidth  = width * heightFactor;
            scaledHeight = height * heightFactor;

            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
        }

    }

    UIGraphicsBeginImageContextWithOptions(targetSize, YES, 1.0);

    dispatch_sync(dispatch_queue_create("pro.idev.itux.projectNameHere", DISPATCH_QUEUE_SERIAL), ^{
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGContextSetInterpolationQuality(ctx, quality);
        [sourceImage drawInRect:(CGRect){thumbnailPoint, {scaledWidth, scaledHeight}}];
    });

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

// Draw CGImage in rect (PNG 1326ms, JPG 1026ms for 8 pictures)
+ (UIImage *)imageWithCGImage:(CGImageRef)sourceImage
scaledToSizeWithSameAspectRatio:(CGSize)targetSize
       withInterpolationQuality:(CGInterpolationQuality)quality
{

    CGFloat width = CGImageGetWidth(sourceImage);
    CGFloat height = CGImageGetHeight(sourceImage);

    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;

    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;

    CGPoint thumbnailPoint = (CGPoint){0.0, 0.0};

    if (CGSizeEqualToSize((CGSize){width, height}, targetSize) == NO) {

        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;

        if (widthFactor > heightFactor) {

            scaledWidth  = width * widthFactor;
            scaledHeight = height * widthFactor;

            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
        }
        else {

            scaledWidth  = width * heightFactor;
            scaledHeight = height * heightFactor;

            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
        }

    }

    UIGraphicsBeginImageContextWithOptions(targetSize, YES, 1.0);

    dispatch_sync(dispatch_queue_create("pro.idev.itux.projectNameHere", DISPATCH_QUEUE_SERIAL), ^{
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGContextTranslateCTM(ctx, 0, targetHeight);
        CGContextScaleCTM(ctx, 1.0, -1.0);
        CGContextSetInterpolationQuality(ctx, quality);
        CGContextDrawImage(ctx, (CGRect){thumbnailPoint, {scaledWidth, scaledHeight}}, sourceImage);
    });

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}
4

0 回答 0