CGImage:创建所需大小的缩略图

发布于 2024-08-11 23:16:18 字数 885 浏览 7 评论 0原文

我想使用 Core Graphics 创建缩略图。缩略图的大小应为 1024 [原文如此],且宽高比与原始图像相同。是否可以直接在 Core Graphics 中设置所需的缩略图大小?

在下面的选项字典中,我可以传递要创建的缩略图的最大尺寸,但是有什么方法可以传递最小尺寸吗?

 NSURL * url = [NSURL fileURLWithPath:inPath];
 CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
 CGImageRef image=nil;
 if (source)
 {
  NSDictionary* thumbOpts = [NSDictionary dictionaryWithObjectsAndKeys:
           (id) kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailWithTransform, 
           (id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailFromImageIfAbsent,
           [NSNumber numberWithInt:2048],  kCGImageSourceThumbnailMaxPixelSize,
           
           nil];
  
  image = CGImageSourceCreateThumbnailAtIndex(source, 0, (CFDictionaryRef)thumbOpts);   
  
  NSLog(@"image width = %d %d", CGImageGetWidth(image), CGImageGetHeight(image));
  CFRelease(source);
 }

I want to create a thumbnail image using Core Graphics. The thumbnail should be size 1024 [sic] with the same aspect ratio as the original image. Is it possible to set the desired size for the thumbnail directly in Core Graphics?

In the options dictionary below, I can pass the max size of the thumbnail to be created, but is there any way to pass a minimum size?

 NSURL * url = [NSURL fileURLWithPath:inPath];
 CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
 CGImageRef image=nil;
 if (source)
 {
  NSDictionary* thumbOpts = [NSDictionary dictionaryWithObjectsAndKeys:
           (id) kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailWithTransform, 
           (id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailFromImageIfAbsent,
           [NSNumber numberWithInt:2048],  kCGImageSourceThumbnailMaxPixelSize,
           
           nil];
  
  image = CGImageSourceCreateThumbnailAtIndex(source, 0, (CFDictionaryRef)thumbOpts);   
  
  NSLog(@"image width = %d %d", CGImageGetWidth(image), CGImageGetHeight(image));
  CFRelease(source);
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

め可乐爱微笑 2024-08-18 23:16:18

如果您想要尺寸为 1024(最大尺寸)的缩略图,则应该传递 1024,而不是 2048。此外,如果您想确保按照您的规范创建缩略图,您应该要求 kCGImageSourceCreateThumbnailFromImageAlways,而不是 kCGImageSourceCreateThumbnailFromImageIfAbsent,因为后者可能会导致使用现有的缩略图,并且它可能比您想要的要小。

所以,这里的代码可以满足您的要求:

NSURL* url = // whatever;
NSDictionary* d = [NSDictionary dictionaryWithObjectsAndKeys:
                   (id)kCFBooleanTrue, kCGImageSourceShouldAllowFloat,
                   (id)kCFBooleanTrue, kCGImageSourceCreateThumbnailWithTransform,
                   (id)kCFBooleanTrue, kCGImageSourceCreateThumbnailFromImageAlways,
                   [NSNumber numberWithInt:1024], kCGImageSourceThumbnailMaxPixelSize,
                   nil];
CGImageSourceRef src = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
CGImageRef imref = CGImageSourceCreateThumbnailAtIndex(src, 0, (CFDictionaryRef)d);
// memory management omitted

If you want a thumbnail with size 1024 (maximum dimension), you should be passing 1024, not 2048. Also, if you want to make sure the thumbnail is created to your specifications, you should be asking for kCGImageSourceCreateThumbnailFromImageAlways, not kCGImageSourceCreateThumbnailFromImageIfAbsent, since the latter might cause an existing thumbnail to be used, and it could be smaller than you want.

So, here's code that does what you ask:

NSURL* url = // whatever;
NSDictionary* d = [NSDictionary dictionaryWithObjectsAndKeys:
                   (id)kCFBooleanTrue, kCGImageSourceShouldAllowFloat,
                   (id)kCFBooleanTrue, kCGImageSourceCreateThumbnailWithTransform,
                   (id)kCFBooleanTrue, kCGImageSourceCreateThumbnailFromImageAlways,
                   [NSNumber numberWithInt:1024], kCGImageSourceThumbnailMaxPixelSize,
                   nil];
CGImageSourceRef src = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
CGImageRef imref = CGImageSourceCreateThumbnailAtIndex(src, 0, (CFDictionaryRef)d);
// memory management omitted
阳光的暖冬 2024-08-18 23:16:18

Swift 3 版本的答案:

func loadImage(at url: URL, maxDimension max: Int) -> UIImage? {
    
    guard let imageSource = CGImageSourceCreateWithURL(url as CFURL, nil)
        else {
            return nil
    }

    let options: [CFString: Any] = [
        kCGImageSourceShouldAllowFloat: true,
        kCGImageSourceCreateThumbnailWithTransform: true,
        kCGImageSourceCreateThumbnailFromImageAlways: true,
        kCGImageSourceThumbnailMaxPixelSize: max
    ]
    
    guard let thumbnail = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, options as CFDictionary)
        else {
            return nil
    }
    
    return UIImage(cgImage: thumbnail)
}

Swift 3 version of the answer:

func loadImage(at url: URL, maxDimension max: Int) -> UIImage? {
    
    guard let imageSource = CGImageSourceCreateWithURL(url as CFURL, nil)
        else {
            return nil
    }

    let options: [CFString: Any] = [
        kCGImageSourceShouldAllowFloat: true,
        kCGImageSourceCreateThumbnailWithTransform: true,
        kCGImageSourceCreateThumbnailFromImageAlways: true,
        kCGImageSourceThumbnailMaxPixelSize: max
    ]
    
    guard let thumbnail = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, options as CFDictionary)
        else {
            return nil
    }
    
    return UIImage(cgImage: thumbnail)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文