CGImage创建具有所需大小的缩略图图像

Dha*_*raj 9 size core-graphics thumbnails

我想用CG创建缩略图.它会创建缩略图.

在这里,我想要缩放1024的大小(纵横比.)是否可以直接从CG获得所需的大小缩略图?

在选项字典中我可以传递thumnail的最大大小可以创建,但有没有任何方法可以有相同的最小尺寸..?

 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);
 }
Run Code Online (Sandbox Code Playgroud)

mat*_*att 20

如果你想要一个大小为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
Run Code Online (Sandbox Code Playgroud)