English 中文(简体)
CGImage create thumbnail image with desired size
原标题:

I want to create the thumbnail using the CG. It creates the thumbnails.

Here i want to have the thumbnail with the size 1024 (with aspect ratio.) Is it possible to get the desired size thumbnail directly from the CG?

In the options dictionary i can pass the max size of the thumnail can be created, but is there any way to have min size for the same..?

 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);
 }
最佳回答

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
问题回答

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




相关问题
How to access thumbnail cache of vista and 7 using c#

I wanted to access the thumb cache of vista and 7 to be used in my ImageList. I know how to do it in XP by means of the thumbs.db files, but in vista and 7 the thumbs.db is not present so how will i ...

User-Defined Thumnails for Django Project?

I m putting together a Web site that makes heavy use of images. Those images need to be thumbnailed at various sizes to fit different templates. I m aware of solutions like sorl-thumbnail, which ...

How to thumbnail faster in c#

I m trying to thumb an image as fast as possible regardless of the usage of resources to be used in my ImageList and listview and this is currently how i m doing it but it seems to be slow: public ...

CGImage create thumbnail image with desired size

I want to create the thumbnail using the CG. It creates the thumbnails. Here i want to have the thumbnail with the size 1024 (with aspect ratio.) Is it possible to get the desired size thumbnail ...

IThumbnailProvider and IInitializeWithItem

I am trying to develop an IThumbnailProvider for use in Windows 7. Since this particular thumbnail would also be dependant on some other files in the same directory, I need to use something other than ...

热门标签