+ (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();
}];
}
謝謝!看起來就像我在找什麼! – 2011-02-12 04:50:40
這在iOS7中不再適用。請參閱下面的n13答案。此外,請參閱此處接受的答案:http://stackoverflow.com/questions/5518790/downloading-normal-image-vs-retina-device-image-2x – RajV 2014-01-27 19:34:53