美文网首页
iOS 大图缩小

iOS 大图缩小

作者: gaookey | 来源:发表于2020-11-23 20:10 被阅读0次
    - (UIImage *)downsampleImageAt:(NSURL *)imageURL to:(CGSize)pointSize scale:(CGFloat)scale {
        // 利用图像文件地址创建 image source
        NSDictionary *imageSourceOptions =
        @{
            (__bridge NSString *)kCGImageSourceShouldCache: @NO // 原始图像不要解码
        };
        CGImageSourceRef imageSource = CGImageSourceCreateWithURL((__bridge CFURLRef)imageURL, (__bridge CFDictionaryRef)imageSourceOptions);
        // 下采样
        CGFloat maxDimensionInPixels = MAX(pointSize.width, pointSize.height) * scale;
        NSDictionary *downsampleOptions =
        @{
            (__bridge NSString *)kCGImageSourceCreateThumbnailFromImageAlways: @YES,
            (__bridge NSString *)kCGImageSourceShouldCacheImmediately: @YES, // 缩小图像的同时进行解码
            (__bridge NSString *)kCGImageSourceCreateThumbnailWithTransform: @YES,
            (__bridge NSString *)kCGImageSourceThumbnailMaxPixelSize: @(maxDimensionInPixels)
        };
        CGImageRef downsampledImage = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, (__bridge CFDictionaryRef)downsampleOptions);
        UIImage *image = [[UIImage alloc] initWithCGImage:downsampledImage];
        CGImageRelease(downsampledImage);
        CFRelease(imageSource);
        return image;
    }
    

    相关文章

      网友评论

          本文标题:iOS 大图缩小

          本文链接:https://www.haomeiwen.com/subject/gmgviktx.html