2010-11-12 9 views
18

जैसा कि आप जानते हैं कि आईफोन दिशानिर्देश 1024x1024 से अधिक लोड होने वाले uiimages को हतोत्साहित करते हैं।स्मृति में लोड किए बिना UIImage गुणों को एक्सेस करने के लिए छवि

छवियों का आकार जो मुझे लोड करना होगा, अलग-अलग होता है, और मैं उस छवि का आकार देखना चाहता हूं जिसे मैं लोड करने वाला हूं; हालांकि uiimage की .size संपत्ति का उपयोग करने के लिए छवि को लॉज किया जाना आवश्यक है ... जो वही है जो मैं टालने की कोशिश कर रहा हूं।

क्या मेरे तर्क में कुछ गड़बड़ है या क्या इसका कोई समाधान है?

आप सभी

+1

यह एक अच्छा सवाल है। एंड्रॉइड ऐसा करने का साधन प्रदान करता है, लेकिन मुझे आईओएस समाधान के बारे में पता नहीं है। संपादित करें: इससे पहले पूछा गया है। http://stackoverflow.com/questions/1551300/get-size-of-image-without-loading-in-to- स्मृति – Justin

+0

मैंने पहले खोज की, लेकिन मुझे यह नहीं मिला! बहुत धन्यवाद! – koda

उत्तर

32

धन्यवाद iOS 4.0 के रूप में, iOS SDK का CGImageSource... functions (ImageIO ढांचे में) शामिल हैं। छवि को स्मृति में लोड किए बिना मेटाडेटा से पूछताछ करने के लिए यह एक बहुत ही लचीला एपीआई है।

#import <ImageIO/ImageIO.h> 

NSURL *imageFileURL = [NSURL fileURLWithPath:...]; 
CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)imageFileURL, NULL); 
if (imageSource == NULL) { 
    // Error loading image 
    ... 
    return; 
} 

CGFloat width = 0.0f, height = 0.0f; 
CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, NULL); 

CFRelease(imageSource); 

if (imageProperties != NULL) { 

    CFNumberRef widthNum = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelWidth); 
    if (widthNum != NULL) { 
     CFNumberGetValue(widthNum, kCFNumberCGFloatType, &width); 
    } 

    CFNumberRef heightNum = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight); 
    if (heightNum != NULL) { 
     CFNumberGetValue(heightNum, kCFNumberCGFloatType, &height); 
    } 

    // Check orientation and flip size if required 
    CFNumberRef orientationNum = CFDictionaryGetValue(imageProperties, kCGImagePropertyOrientation); 
    if (orientationNum != NULL) { 
     int orientation; 
     CFNumberGetValue(orientationNum, kCFNumberIntType, &orientation); 
     if (orientation > 4) { 
      CGFloat temp = width; 
      width = height; 
      height = temp; 
     } 
    } 

    CFRelease(imageProperties); 
} 

NSLog(@"Image dimensions: %.0f x %.0f px", width, height); 

(Gelphman और लादेन "क्वार्ट्ज साथ प्रोग्रामिंग" से अनुकूलित 9.5, पेज 228 लिस्टिंग,: एक छवि के पिक्सेल आयाम हो रही इस तरह काम करना चाहिए (अपने लक्ष्य में ImageIO.framework शामिल करना सुनिश्चित करें))

+2

केवल 'CGImageSourceCopyProperties' के बजाय 'CGImageSourceCopyPropertiesAtIndex' का उपयोग क्यों करें? – jcm

+3

चरम सीमा 'चौड़ाई' और 'ऊंचाई' को' CGFloat 'के रूप में घोषित किए जाने के बाद CFNumberGetValue() को कॉल करते समय' केसीएफएनंबरएफओएलटी टाइप 'के बजाय' केसीएफएनंबरसीजीएफओटीटी टाइप 'को पास करना बहुत महत्वपूर्ण है। उपरोक्त कोड 32-बिट सिस्टम पर काम करेगा लेकिन मानों में 64-बिट सिस्टम पर कचरा होगा। – fjoachim

+0

@fjoachim: धन्यवाद, तय है। –

3

स्विफ्ट 3 जवाब के संस्करण:

import Foundation 
import ImageIO 

func sizeForImage(at url: URL) -> CGSize? { 

    guard let imageSource = CGImageSourceCreateWithURL(url as CFURL, nil) 
     , let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as? [AnyHashable: Any] 
     , let pixelWidth = imageProperties[kCGImagePropertyPixelWidth as String] 
     , let pixelHeight = imageProperties[kCGImagePropertyPixelHeight as String] 
     , let orientationNumber = imageProperties[kCGImagePropertyOrientation as String] 
     else { 
      return nil 
    } 

    var width: CGFloat = 0, height: CGFloat = 0, orientation: Int = 0 

    CFNumberGetValue(pixelWidth as! CFNumber, .cgFloatType, &width) 
    CFNumberGetValue(pixelHeight as! CFNumber, .cgFloatType, &height) 
    CFNumberGetValue(orientationNumber as! CFNumber, .intType, &orientation) 

    // Check orientation and flip size if required 
    if orientation > 4 { let temp = width; width = height; height = temp } 

    return CGSize(width: width, height: height) 
} 
संबंधित मुद्दे

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