2011-02-12 17 views
15

के लिए यूआरएल से छवि मेरे पास एक ऐसा एप्लीकेशन है जो NSURL से छवियों को खींचता है। क्या यह एप्लिकेशन को सूचित करना संभव है कि वे रेटिना ('@ 2x') संस्करण हैं (छवियां रेटिना रिज़ॉल्यूशन के हैं)? मैं वर्तमान में निम्नलिखित लेकिन छवियों उच्च संकल्प प्रदर्शित करता है पर pixelated दिखाई देते हैं:रेटिना डिस्प्ले

NSURL *url = [NSURL URLWithString:self.imageURL]; 
NSData *data = [NSData dataWithContentsOfURL:url]; 
UIImage *image = [UIImage imageWithData:data]; 
self.pictureImageView.image = image; 

उत्तर

11

imageWithData:scale: (iOS 6 और बाद में) का उपयोग करके देखें

NSData *imageData = [NSData dataWithContentsOfURL:url]; 
UIImage *image = [UIImage imageWithData:imageData scale:[[UIScreen mainScreen] scale]]; 
+0

यह 3x छवियों के लिए भी काम करता है? (मुझे ऐसा लगता है कि '[यूआईस्क्रीन मुख्यस्क्रीन] पैमाने] ') – androniennn

0

@ 2x सम्मेलन आवेदन बंडल से लोड हो रहा है छवियों के लिए सिर्फ आसान तरीका है।

छवि का आकार 100x100

देखें आकार: 50x50 आप रेटिना प्रदर्शन पर छवि दिखाने के लिए wan't हैं तो आप यह 2x बड़ा करने के लिए किया है।

संपादित करें: मुझे लगता है कि आप सबसे अच्छा समाधान के लिए कुछ अतिरिक्त परम (जैसे पैमाने) जोड़ने हो सकता है और उपयुक्त आकार की छवियों को वापसी होगी सर्वर से छवियों को लोड कर रहे हैं:

www.myserver.com/get_image.php?image_name=img.png&scale=2 

आप का उपयोग कर पैमाने प्राप्त कर सकते हैं [ [यूआईस्क्रीन मुख्य स्क्रीन] स्केल]

17

आपको छवि दृश्य में जोड़ने से पहले UIImage को पुन: सहेजने की आवश्यकता है।

NSURL *url = [NSURL URLWithString:self.imageURL]; 
NSData *data = [NSData dataWithContentsOfURL:url]; 
UIImage *image = [UIImage imageWithData:data]; 
CGFloat screenScale = [UIScreen mainScreen].scale; 
if (image.scale != screenScale) 
    image = [UIImage imageWithCGImage:image.CGImage scale:screenScale orientation:image.imageOrientation]; 
self.pictureImageView.image = image; 

स्केल मान को कड़ी-कोडिंग से बचने के लिए सबसे अच्छा है, इस प्रकार यूआईस्क्रीन कॉल। यह आवश्यक क्यों है इसके बारे में अधिक जानकारी के लिए UIImage’s scale property पर ऐप्पल के दस्तावेज़ देखें।

NSData की -dataWithContentsOfURL: विधि (जब तक आपका कोड पृष्ठभूमि थ्रेड पर नहीं चल रहा हो) का उपयोग करने से बचने के लिए भी सबसे अच्छा है, क्योंकि यह एक सिंक्रोनस नेटवर्क कॉल का उपयोग करता है जिस पर निगरानी या रद्द नहीं किया जा सकता है। आप सिंक्रोनस नेटवर्किंग के दर्द और this Apple Technical Q&A में इससे बचने के तरीकों के बारे में अधिक पढ़ सकते हैं।

+0

धन्यवाद! ऐसा लगता है कि मैं क्या देख रहा था! –

+0

यह आईओएस 7 में और काम नहीं करता है। नीचे काम करता है n13 का जवाब देखें। साथ ही, यहां स्वीकृत उत्तर देखें: http://stackoverflow.com/questions/5518790/downloading-normal-image-vs-retina-device-image-2x – RajV

1

बस इसमें जोड़ने के लिए, मैंने विशेष रूप से निम्नलिखित किया था, उसी स्थिति में, एक आकर्षण की तरह काम करता है।

double scaleFactor = [UIScreen mainScreen].scale; 
     NSLog(@"Scale Factor is %f", scaleFactor); 
     if (scaleFactor==1.0) { 
      [cell.videoImageView setImageWithURL:[NSURL URLWithString:regularThumbnailURLString]; 
     }else if (scaleFactor==2.0){ 
      [cell.videoImageView setImageWithURL:[NSURL URLWithString:retinaThumbnailURLString]; 
     } 
5

आपको UIImage पर स्केल सेट करने की आवश्यकता है।

UIImage* img = [[UIImage alloc] initWithData:data]; 
CGFloat screenScale = [UIScreen mainScreen].scale; 
if (screenScale != img.scale) { 
    img = [UIImage imageWithCGImage:img.CGImage scale:screenScale orientation:img.imageOrientation]; 
} 

प्रलेखन अन्यथा आप अजीब प्रदर्शन समस्याएं हो सकती है जहां चीजें आधे आकार, डबल आकार, आधा संकल्प, वगैरह पर दिखाने के लिए, एक ही पैमाने पर अपने सभी UIImages के निर्माण के लिए सावधान रहना होगा कहते हैं। उन सभी से बचने के लिए, रेटिना रिज़ॉल्यूशन पर सभी UIImages लोड करें। संसाधन स्वचालित रूप से सही पैमाने पर लोड हो जाएंगे। यूआरएल डेटा से निर्मित यूआईएममेज के लिए, आपको इसे सेट करने की आवश्यकता है।

0
+ (void)deviceScaledImageForView:(UIImageView*)v 
     withParameterizedUrl:(NSString*)url 
       iPadSupport:(BOOL)iPadSupport 
         after:(UITask)after 
{ 

// ...Declare Retina and Retina Plus versions of the the URL string... 
NSString* url2x = @""; 
NSString* url3x = @""; 

// ...And if I'm on an iPad with my caller allowing iPad-specific images... 
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && iPadSupport) { 
    // ...Generate the iPad non-retina and retina versions of the URL... 
    /* Note 3x is not applicable to iPad */ 
    url = [url stringByReplacingOccurrencesOfString:@".png" withString:@"~ipad.png"]; 
    url2x = [url stringByReplacingOccurrencesOfString:@"~ipad.png" withString:@"@2x~ipad.png"]; 
} 
// ...Or, for iPhone... 
else { 
    // ...Generate the iPhone non-Retina, Retina and Retina Plus versions of the URL... 
    url2x = [url stringByReplacingOccurrencesOfString:@".png" withString:@"@2x.png"]; 
    url3x = [url stringByReplacingOccurrencesOfString:@".png" withString:@"@3x.png"]; 
} 

// ...If I'm running on iOS 7.1 or newer... 
CGFloat scale = .0f; 
UIScreen* screen = [UIScreen mainScreen]; 
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_7_1) 
    // ...Choose a scale based on the iOS 8 API. 

    //currently the scale for the 6 plus ranges from 2.6 to 2.8 to 3.0 depending on zoomed mode or running on the sim 
    // since our images are at 2x or at 3x we want whole numbers. 
    scale = ceilf(screen.nativeScale); 
else 
    // ...Choose a device scale on the iOS 7 API. 
    scale = screen.scale; 

// ...If I've chosen a Retina Plus scale... 
if (scale > 2.0f){ 
    // ...Select the 3x Retina Plus version of the image. 
    url = url3x; 
} 

// ...Otherwise for retina displays... 
else if (scale == 2.0f) 
    // ...Select the Retina version of the image. 
    url = url2x; 

// ...And finally, request the image data with SDWebImage (for cache support)... 
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:url] 
                 options:SDWebImageDownloaderUseNSURLCache 
                progress:nil 
                completed:^(UIImage* i, 
                   NSData* d, 
                   NSError* e, 
                   BOOL finished) 
{ 
    // ...And after the image is obtained... 
    // ...Apply it to the image view with the correct device scale... 
    v.image = [UIImage imageWithData:d scale:scale]; 

    // ...And if I have an after-action... 
    if (after) 
     // ...Run it. 
     after(); 
}]; 

} 
0

iPhone बताने के लिए रेटिना प्रोग्राम के रूप में है कि विशेष छवि है, तो आप कुछ इस तरह कर सकते हैं:

UIImage *img = [self getImageFromDocumentDirectory]; 
img = [UIImage imageWithCGImage:img.CGImage scale:2 orientation:img.imageOrientation]; 

मेरे मामले में, TabBarItem छवि गतिशील थी यानी सर्वर से डाउनलोड हो रहा था। फिर आईओएस इसे रेटिना के रूप में नहीं पहचान सकता है। उपरोक्त कोड स्निपेट ने मेरे लिए एक आकर्षण की तरह काम किया।

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