views:

298

answers:

2

Hi folks,

I need to initialise images from raw data downloaded from a server which delivers the correct size of image based on the type of iPhone client.

I know that I should be setting the scale value to be 2.0 on the 640x960 display, however this is a readonly property and cannot be set during the init when using initWithData.

Any ideas?

A: 

AFAIK you don't need to set the scale value yourself. The OS will handle the points to pixel translation for you.

Rengers
If I just initialise a UIImage from data, it is up-scaled on the iPhone 4. (eg, a 320x480 image will be fullscreen on iPhone 4). I need to prevent this somehow so I can use high resolution images
Sam
Ah ok didn't know that, thanks!
Rengers
+5  A: 

I'm not aware of anything you can embed in the image data itself to tell the phone that it's a @2x image, but something like this should work:

UIImage * img = ...;
img = [UIImage imageWithCGImage:img.CGImage scale:2 orientation:img.imageOrientation];
tc.
Perfect, thank you
Sam
This fixed it for me too, thanks! I was drawing text on an image and needed this to keep the scale correct on the resulting image.
Jason
Perfekt for me too... But why do we have to implement such low level stuff if we store an image with UIImagePNGrepresentation and want it back correctly?? That's not Apple-like. CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData((CFDataRef)<storedPNGData (NSData)>); CGImageRef cgHeaderImage= CGImageCreateWithPNGDataProvider(dataProvider,NULL,NO, kCGRenderingIntentDefault); CGDataProviderRelease(dataProvider); headerCell.headerImage.image = [UIImage imageWithCGImage:cgHeaderImage scale:2.0 orientation:UIImageOrientationUp];CGImageRelease(cgHeaderImage);
Gerd
Because the PNGRepresentation has no notion of "scale" (similarly, the JPEGRepresentation has no notion of transparency); the image is simply loaded by CGImage. While in theory you could use image DPI, I'm pretty sure that CGImage simply ignores DPI information, so UIImage would have to do additional parsing. Additionally, DPI information is *almost always meaningless*: Macs default to 72, Windows defaults to 96, and most digital cameras default to 300 (and the "DPI" of a photo is completely meaningless; I want angular resolution/FOV and lens distortion curve).
tc.
I'd also just use `UIImage * img = [UIImage imageWithData:...]; img = [UIImage imageWithCGImage:img.CGImage, scale:2 orientation:img.imageOrientation];`. And finally, `img = [UIImage imageWithData:UIImagePNGRepresentation(img)]` isn't an indentity transform; UIImagePNGRepresentation will often have to convert from premultiplied alpha to "normal" alpha.
tc.