2009-11-19 6 views
8

के साथ थंबनेल छवि बनाएं, मैं सीजी का उपयोग करके थंबनेल बनाना चाहता हूं। यह थंबनेल बनाता है।CGImage वांछित आकार

यहां मैं आकार 1024 (पहलू अनुपात के साथ) के साथ थंबनेल रखना चाहता हूं। क्या सीजी से वांछित आकार थंबनेल प्राप्त करना संभव है?

विकल्प शब्दकोश में मैं 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); 
} 

उत्तर

18

आप आकार 1024 (अधिकतम आयाम) के साथ एक थंबनेल चाहते हैं, आप 1024 2048. इसके अलावा चाहिए गुजर जाना चाहिए, नहीं, आपको यह सुनिश्चित थंबनेल आपके विनिर्देशों के बनाई गई है बनाना चाहते हैं, तो आप के लिए पूछ kCGImageSourceCreateThumbnailFromImage हमेशा, 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 
2

स्विफ्ट 3 संस्करण:

func loadImage(at url: URL, maxDimension max: Int) -> UIImage? { 

    guard let imageSource = CGImageSourceCreateWithURL(url as CFURL, nil) 
     else { 
      return nil 
    } 

    let options = [ 
     kCGImageSourceShouldAllowFloat as String: true as NSNumber, 
     kCGImageSourceCreateThumbnailWithTransform as String: true as NSNumber, 
     kCGImageSourceCreateThumbnailFromImageAlways as String: true as NSNumber, 
     kCGImageSourceThumbnailMaxPixelSize as String: max as NSNumber 
    ] as CFDictionary 

    guard let thumbnail = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, options) 
     else { 
      return nil 
    } 

    return UIImage(cgImage: thumbnail) 
} 

तो, यहाँ कोड है कि आप क्या पूछना है

संबंधित मुद्दे