views:

214

answers:

3

I'm trying to set the size of a UIImageView programmatically using:

[thisImageView setFrame:CGRectMake(x, y, image.size.width, image.size.height)];

I tried using x = 0, y = 0, but the UIImageView disappears.

More info on why I'm trying to do this. I'm trying to display a high-res image on an iPhone 4 without using the iOS 4 SDK (only using 3.2 SDK API calls). I have calls to see if I'm on an iPhone 4, so I'm trying to double the size of my UIImageView so that when I put the high-res UIImage in, it won't be grainy.

+1  A: 

I think your approach is wrong. iPhone 4 works with points instead of pixels, which makes doubling the size troublesome. 1 point on old devices equals 4 pixels on iPhone 4, but is still used as 1 point.

When you create the frame you're not actually giving the API the pixel dimensions, but the point dimensions.

Accatyyc
So is there no way to do this without using the 4.0 SDK?
mark
Except an iPhone "point" is a lot smaller than the standard (PostScript) point of 1/72 inches. While Apple may call it a "point", it's a *terrible* name.
tc.
+1  A: 

Not sure what there is so special about iOS. Just use the same method you use with 3.2: imageNamed: ... it will take care of everything as long as your images are named properly (yourimagename.png and [email protected]). Nothing fancy in the iOS 4 SDK here, just give the size you use in points as before.

If this is not suitable in your case, can you explain why?

Eiko
Yes Eiko. The problem here is I am retrieving the image from a web server so I can dynamically change the image. The image I am sending is at the resolution I want to display on the iPhone 4 (e.g. my UIImageView in IB is 100 by 100 pixels and I am sending down an image that is 200 by 200 pixels). I tried sending down the image with the @2x suffix, but it didn't help. Would a solution be to send down two versions of the image, store them locally and then load it?
mark
Hmm... "maybe". Downloading the image, saving it and using imageNamed: should work (don't use the @2x extension in that method, just the plain name). You should also be able to set the *scale factor* of the image manually.
Eiko
Setting the scale manually is a 4.0 SDK feature. :/
mark
But there are no devices < 4.0 with retina display anyway?!
Eiko
I think you're confusing the SDK version with the OS version. iOS 4.0 (the OS) can run apps written using only the 3.2 SDK. Yet, I don't have access to all the nice functionality that is useful for retina displays in the 3.2 SDK even though I am running my apps on iOS 4.0+ devices.
mark
You should compile *every* new app with the latest (read: 4.1) SDK. This also applies if you target 3.x devices. (And IIRC Apple only accepts builds with 4.x SDK as application uploads now)
Eiko
+2  A: 

I'm trying to display a high-res image on an iPhone 4 without using the iOS 4 SDK (only using 3.2 SDK API calls)

Ouch. Don't do that.

Compile against 4.0, set the deployment target to 3.whatever.

When creating the image, check [UIImage respondsToSelector:@selector(imageWithCGImage:scale:orientation:)]. If it does, then you can use +[UIImage imageWithCGImage:scale:orientation:] to create a "scale 2" image, which will display correctly in a UIImageView on iPhone 4 (which has a "scale 2" screen).

If you really can't use the OS 4 SDK (you don't seem to state a reason why), then make the UIImageView smaller using the appropriate content mode (AspectFit/AspectFill/ScaleToFill), and set its frame to half the size in pixels (e.g. if the image is 100x100 px, set it to (CGSize){50,50}). This shrinks the image to half-size, but the double-res screen means you see it pixel-for-pixel.

tc.
If I use the 4.0 SDK, it enables backgrounding which breaks some functionality in my app. So I have to build and use the 3.2 SDK for now. Can you give me an example of your method for not using the OS 4 SDK? Thanks!
mark
If app backgrounding is the only thing that you don't want in 4.0, just set `<key>UIApplicationExitsOnSuspend</key><true/>` in Info.plist.
tc.
Thanks tc - that looks interesting. Would it be okay for you to edit your post and show me an example of your no-4.0 SDK solution? Am I doing everything you specify if I am running on an iPhone 4? Which UIImageView do I change? The one in IB or programmatically when I check to see if I am running on an iPhone 4?
mark