obj-c images resizing automatically...? - ios

I just started learning obj-c the other day and i'm putting together a crappy game as practice. I have an IMG as a main character to the game and every time this character hits certain obstacles, i wanted the size of the image to change (decrease or increase depending on the circumstance).
I used photoshop the change the size of the images to the appropriate sizes but for some reason, when i run the game, the images change when i want them too, but they are way smaller than the size i set them to in photoshop......
any ideas?
i don't think its necessary to post my code for something as simple as this, right? its just a simple "if" statement followed by the instance and the UIimage named for the image name...

I assume you display the image in an UIImageView? Or do you use SpriteKit? If you use an UIImageView the image is actually automatically scaled to the size of the UIImageView.
Therefore you would just change the size of the UIImageView (you can of course also change the image inside the UIImageView.
If you are using SpriteKit, you have to remember, that your are probably testing on a 'retina' device and for this reason the image's width and height is divided by 2 (the real resolution of the current iPhone 5/5s is 1136x640 and not 568x320 !

Related

supplying the right image size when not knowing what the size will be at runtime

I am displaying a grid of images (3rows x 3 columns) in collection view. Each image is a square and its width is determined to be 1/3 of collectionView's width. Collection view is pinned to left and right margin of the mainView.
I do not know what the image height and width will be at runtime, because of different screen sizes of various iPhones. For example each image will be 100x100 display pixels on 5S, but 130x130 on 6+. I was advised to supply images that exactly matches the size on screen. Bigger images often tend to become pixelate and too sharp when downsized. How does one tackle such problem?
The usual solution is to supply three versions, for single-, double-, and triple-resolution screens, and downsize in real time by redrawing with drawInRect into a graphics context when the image is first needed.
I do not know what the image height and width will be at runtime, because of different screen sizes of various iPhones. For example each image will be 100x100 display pixels on 5S, but 130x130 on 6+
Okay, so your first sentence is a lie. The second sentence proves that you do know what the size is to be on the different screen sizes. Clearly, if I tell you the name of a device, you can tell me what you think the image size should be. So, if you don't want to downscale a larger image at runtime because you don't like the resulting quality, simply supply actual images at the correct size and resolution for every device, and use the correct image on the actual device type you find yourself running on.
If your images are photos or raster type images created using a raster drawing tool, then somewhere you will have to scale the original to the sizes you want. You can either do this while running in iOS, or create sets up front using a tool which can give you better scaling results. Unfortunately, the only perfect image will be the original with everything else being a distortion of the truth.
For icons, the only accurate rendering solution is to use vector graphics. Tools like Adobe Illustrator will let you create images which you can scale to different sizes without losing clarity. Unfortunately this still leaves you generating images up front. You can script this generation using most tools and given you said your images were all square, then the total number needed is not huge. At most you need 3 for iPhone (4/5 are same width, 6 and 6+) and 2 for iPad (#1 for mini/ipad1 and #2 for retina).
Although iOS has no direct support I know of for vector image rendering, there are some 3rd party tools. http://www.paintcodeapp.com/ is an example which seems to let you import vector images or draw vector images and then generate image code to run in your app. This kind of tool would give you what you want as the images are now vector drawings drawn at the scale you choose at run time. $99 though.
There is also the SVGKit (https://github.com/SVGKit/SVGKit), but not sure how good/bad this is. It seems to let you simply load and render direct from SVG files. Might be worth trying.
So in summary, I think you either generate the relatively small subset up front using a tool you can control the output from, take the hit in iOS and let it scale the images or use a 3rd party vector to image rendering kit which would give you what you want.

Changing image/textures of a SKSpriteNode with different sized images

I have a large number of small images that I am swapping between in a SKSpriteNode object. The problem is it seems to establish a set container size and other images will stretch to fit the container. Is there a way to reset this each time? Or do I have to modify all the images to be the same size? I was hoping it could just display them as the true image size and I could keep it centered as they are all already centered as they are.
I basically just update the texture each time I do it. Is there a flag or property I am not setting correctly?
When a SKSpriteNode is first created, its texture/image size is set. I suggest you take the largest x and y values in your animation sequence and set all pics to the same (largest) size. You can use a transparent background for each pic.

how to display image (containing text) with a good quality

I’m new in iOS and trying to make simple app with hierarchy of viewcontrollers. In the last one I wanna display scrollable image (which can also be zoomed at least x1,5), containing some small black and white picture and a piece of text. Initially I planned to make vector image, convert it to .jpg and use UIScrollView for displaying. But I found out that .jpg ( approx. 150 KB) didn’t provide a good quality for displaying text. As I have to use a lot of images I don’t want to increase image size. What is worse I also want it look good on retina display.
Can you recommend a way how to display image, containing text, with enough quality?
I mean that I don’t want the user see the separate pixels of letters in the text. Just like when you read text in your e-mail in iOS. Image size should be as small as possible. Planning physical size of image – approx. 5 cm x 15 cm.
Any help much appreciated
Thanks
To get good edges you would need to use png not jpg, which will make the image sizes much larger. I have a better suggestion, more code but better solution.
The answer is to not put the text into the image, but to draw it over it in real time.
You would:
associate text at some coordinate in the image (say a CGRect) with the image
create a uiimageview subclass that in the drawRect routine, after calling super, draws the text using the NSString categories on UIKit (which let you draw into a context)
To get going on this please create a small one vc project and get the subclass working there, then back port it to your primary project.

How to update an app comprised entirely of images for the iPhone 5

I need to update an app which was written for the iPhone 4 screen size to work on the iPhone 5. I have read on SO about AutoLayout and so on, but the problem is that each screen in this app is made up of a background image, which then has touchable areas and such drawn on in code. These are positioned absolutely. This isn't the way I would have designed the app, but it's the task I am faced with.
I have two problems to solve:
1) How do I load the correct background image. Do I need to create a separate image size for each and them in code query the device size each time an image is loaded? To give some context, there are well over 100 images.
2) How do I maintain the touchable areas. Is it best to just add the required number of pixels to the bottom of the app? Would this then work, or will I need to query device size and change coords accordingly every time I draw something?
Thank you,
Sam
Autolayout will work in the device is running iOS 6 or higer.
1) Yes you will need a image larger image for all the images in your app. You could overload some methods in a base class to make this more easy to load the correct image.
An other option is to stretch the image, but this is up to you.
2) I would adjust my coordinates to the size of the device.
If Apple would add an other screen size you will run in the same problem again. You should try and create an interface that can grow with the screen.

Dynamically resize background image according to different screen size

I displays a splash Screen when my app loads,There is a background image in the splash screen,I problem is how can I make fit this image in all types of blackberry models?
Keep in mind that many BlackBerry devices have different screen resolutions and even different aspect ratios. So if you just use a single image and resize (stretch and/or squish) it to fit the current screen, you're going to distort the image (or pattern). As I see it, there are two main approaches:
1) Use a different image for each screen resolution. There are about 7 different resolutions that cover most of the in-market devices (240x260, 240x320, 320x240, 360x400, 360x480, 480x320, 480x360)
2) If it's a regular background pattern as opposed to a picture or logo, just have one image in the app that's big enough to cover the largest screen size (480x360) and for all other screen sizes just clip it. In fact, I think this should happen automatically if you just set the background image - anything that can't be displayed on the screen will be clipped.
While approach #2 is better in terms of reducing application size, I'm going to guess that since you're asking this question the background you're thinking of using isn't a regular pattern.
I think the simplest method would be to use the setBorder method of whatever screen/field needs a stretched background. For example:
Border b = BorderFactory.createBitmapBorder (new XYEdges (), bitmap);
field.setBorder(b);
In my experience this results in the background image being stretched and provides the simplest method for fitting the size you need. I have only ever used it for fields though and never a MainScreen so it might not work for you.

Resources