开发者

Selectively loading low-resolution resources in iOS when @2x-version is also present

开发者 https://www.devze.com 2023-04-01 12:52 出处:网络
I had a lot of trouble trying to force the loading of the low-resolution version of some resources on iOS, when the high-res version (@2x) is also present.

I had a lot of trouble trying to force the loading of the low-resolution version of some resources on iOS, when the high-res version (@2x) is also present.

The reason I wanted to do this is simple: My app shows a bunch of full-screen images to the user (320x480 on older devices, 640x960 on Retina devices), and the user can upload some of these to TwitPic. I wanted to unify the uploaded images to 320x480, regardless of the device (for consistency, because that size is fine in a web browser).

I found out that no matter what UIImage method I used, when the high-resolution version of a resource file is present it is loaded (even if you pass the full file path): UIImage will out-smart you. But I found a way to out-smart UIImage:

- (UIImage*) lowResImage{

    N开发者_如何学PythonSString* path = [[NSBundle mainBundle] pathForResource:_fileName 
                                                     ofType:@"png"
                                                inDirectory:_resourceSubdirectory];

    path = [path stringByReplacingOccurrencesOfString:@"@@2x" withString:@""];

    NSData* imageData = [[NSData alloc] initWithContentsOfFile:path];

    UIImage* image = [[UIImage alloc] initWithData:imageData];
    [imageData release];

    return [image autorelease];
}

The above code works fine on a 4th generation iPod touch.

Anybody came up with a better approach?


Build your path, open data, and then init the image using data as in the example below.


   NSString *dataPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:_fileName];
   NSData *data = [[NSData alloc] initWithContentsOfFile:dataPath];
   UIImage *image3 = [UIImage imageWithData:data];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号