【问题标题】:CGImage create thumbnail image with desired sizeCGImage 创建所需大小的缩略图
【发布时间】:2010-12-18 04:42:24
【问题描述】:

我想使用 CG 创建缩略图。它创建缩略图。

这里我想要尺寸为 1024(纵横比)的缩略图。是否可以直接从 CG 中获得所需尺寸的缩略图?

在选项字典中,我可以通过可以创建缩略图的最大大小,但是有没有办法让最小大小相同..?

 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);
 }

【问题讨论】:

    标签: size core-graphics thumbnails


    【解决方案1】:

    如果您想要大小为 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
    

    【讨论】:

    • 目前尚不清楚 OP 是否希望“1024”成为生成缩略图的较小或较大尺寸 - 他只是说他想保留原始图像的纵横比 - 这是无论如何只支持行为。但是,如果 OP 希望 1024 成为“最小尺寸”,他们可以很容易地从原始图像尺寸计算它,如下所示: ratio = MIN(original.size.width, original.size.height) / 1024; maxSize = MAX(original.size.width, original.size.height) * 比例; - 并在字典中使用“maxSize”。
    【解决方案2】:

    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)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-17
      • 2011-12-11
      • 2013-06-22
      • 2013-06-24
      • 1970-01-01
      • 1970-01-01
      • 2012-07-15
      • 2011-03-06
      相关资源
      最近更新 更多