Performance: UIImage vs UIView - ios

Out of sheer interest:
Is there any difference, specifically in (theoretic) performance or memory usage between using a UIImage of 25x25 pixels, square, one color, png on the one hand, or a UIView of the same size and color?
Consider the Unread bullet in Mail.app. Would you use an image for that? Or a UIView with rounded edges?
An image takes more space, and resided in a UIImageview, and has a resolution dependency, but on the other hand, once it is loaded, it wouldn't make too much difference, would it?

If you use UIImageView then it requires image. Where if you use UIView you not need image, so it makes your application light weight. Second thing, large image takes more memory to load. So, it is always beneficial to use UIView instead of image wherever possible! It keeps your application light weight and can give better performance!

An UIImageView, being a subclass of UIView, will instantiate a regular view with all the extension that UIKit developers have built to support displaying an image inside a plain UIView. By doing that you're only creating an extended (aka possibly heavier) version of a standard UIView.
That said, using an UIView for simple UI elements (like Mail.app's bullet icon) will also allow you to forget about the resolution of the graphical asset since you don't have to care about #2x or #3x resolutions resulting also in a smaller project size.
Of course you'll only save kilobytes when it comes to simple shapes, but reusing this pattern all across the app will benefit you exponentially in the long term.

Related

How to reduce app size using app thinning and explain different ways to reduce app size

I have .png images having 1.6 mb. I used few classes having 17 mb memory size but my project size 40 mb approximately. Can any one suggest best solution for reducing app size for uploading into app store.suggest me how to find largest file in my target file.(i used image assets, i don't have unused code).
List of techniques that help you:
Remove Unused code
Generally third party libraries have lots of unused code. Include the code only related to features using in that app. If you find any images, music, extra content that is not using in app should be removed from the project.
Load Resources on Demand
If your project includes resources like tutorial videos or graphics that are expected to be rarely needed, consider moving these onto a web-server somewhere and have your application load them on-demand. Amazon’s S3 service or a small VPS on Linode will typically do the trick quite well. Discretion is needed with this technique since you want to avoid frustrating the user with a massive download after installation.
Use patterned images
UIColor includes a fantastic feature for reducing the need to include large textured images within your project. An example of this is shown above where you want to give your app a stylized background, though this technique applies equally well to smaller areas where you want to add texture to a UI control. Simply set the background color of the desired view to a patterned color like this:
view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"pattern.png"]];
The view now will have a textured background that scales and adjusts with the size of the view. A good source for textures that tile well is the Subtle Patterns project.
Use stretched images
UIImage includes a performant method of creating images that scale dynamically with size. The image is setup so that the middle section stretches keeping the edges unchanged. This is often used for things like buttons where the corners and sides are styled but the body of the button is plain. I also works well for creating an ‘etched’ effect in table views (like in the App Store example above). Here rather than creating a 320x48 image that represents the cell background you create a tiny 1x3 image with the desired top, body and bottom colors desired. This is then assigned as your backgroundView resulting in the effect shown.
UIImage* template = [UIImage imageName:#"template.png"];
UIImage* stretched = [template resizableImageWithCapInsets:UIEdgeInsetsMake(1, 0, 1, 0)]
cell.backgroundView = [[UIImageView alloc] initWithImage:stretched];
you need to use image Assets in you app. Add all images in Assets.xcassets file.

obj-c images resizing automatically...?

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 !

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.

Interface building - UIViews vs Images vs Core graphics vs PDF subclass

My app uses flat graphics, which mostly consists of lines and flat surfaces...
Whats the best approach for building the UI?
use PNG images as much as possible (like for the attached picture, the whole background would be an image)
use 4 UIViews (in this example case) with background color to make the background and the 3 lines
use CoreGraphics to actually draw in drawRect (or somewhere else?)?
subclass UIView and draw PDF content
any other approach?
What are the performance impacts? The advantage of the first two, is that they can be done in IB, but is there a downside (like performance or quality or caching)? I also heard of a trend of using CoreGraphics for drawing all the time...
So basically the fastest way is to use PNG files (stretchable and normal), also if you are using PNG files it will be easy to change the design and fix eventual bugs.
When you are using PNG files, try to reduce the size of the PNGs (stretchable images for backgrounds) for a better performance and a small size of the app.
CoreGraphics is more complex and can add strange bugs or performance problems if you don't know what you are doing, but can be use also for simple view styling.
On every project that I've done I couldn't use only one of these two because of the project complexity or because I was to lazy to open photoshop and add extract some designs.
As H2CO3 said is not that simple. Each format suits particular requirements, there are no written laws, I just ca tell you what I do.
Small pieces of UI, such as buttons, icons etc: I normally use PNG or PDF. You can subclass a UIView to draw PDF vector content. The plus with PDF is that if the source is a vector image, you could not take care about physical-logical points.
Images or Hi-res images, such as image of a content: In this case I use JPG, for they small size respect of PNG. They take little bit more to decompress but usually the size is very smaller compared to PNG.
In general PNG can use alpha, JPG no
Image is a big topic, nowadays I always try a way to find symmetry in UI elements and use stretchable UIImages with PNG.
For you example I will use PNG witch stretchable left and right side. Instead of use 640 px image, you can use 10 px image, with stretchable right margin and stretchable left margin.
[update]
It should be said that if you use stretchable images you will get a lot of performance boost, since the image will be smaller, you will occupy less memory on the "disk", less memory on the heap thatnks to the GPU stretching and less decompressing times
.

iOS: Image sizes in custom UISlider

In our iOS app, we have subclassed a UISlider in order to use a custom design. However, no matter what we try, the images used in the UISlider come out distorted. For instance, the original thumb rect is 156x44. We have overridden this to be 15x22, which results in a blown up image. Other permutations result in the thumb going off-track, and still blown up. How can we correctly set the image size for the thumb and track, by using padding on the images or by overriding the appropriate functions?
I think that UISlider was designed one resolution and one only, and making the elements smaller, will negatively affect user experience. But if you really want to, try one of these:
Use the bigger images, but set the transform property of an UISlider instances to some CGAffineTransformMakeScale(scaleX, scaleY).
Add transparent padding to your images, so they look small, but are actually of a proper size.
That is, if you really want your UISlider smaller. If all you are bothered with is the distortion caused, but automatic upscaling, consider loading UIImages as stretchable, f.i. with
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets // iOS >=5.0
or
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight // iOS <5.0

Resources