Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImageView setImage or setBackgroundColor, which one is better in iOS

I might get down-votes for this question but i am curios and couldn't find any explanation. So I am gonna post it here.

My question is, I have to show custom separator image in my custom cell. If I simply set background colour of imageView, rather than setting UIImage, is it better approach?

Here is the example screenshot to explain my question.

enter image description here

In this screenshot, I have black and grey separators.

[barImage setBackgroundColor:[UIColor blackColor]];
[greyBarImage setBackgroundColor:[UIColor greyColor]];

OR

[barImage setImage:[UIImage imageNamed:@"black.png"]];
[greyBarImage setImage:[UIImage imageNamed:@"grey.png"]];

Now, which one is better? All I know the imageNamed: is bit slower and we should avoid, but I am not sure about that.

EDIT:

This is what I know after posting this question:

Setting Image:

  • It will cost the RAM
  • Image will not cover the whole imageView, so there might be un-opaque portions on UIImageView

Setting background colour:

  • This is an overhead on CPU but will decrease app memory.
  • Background colour will cover the whole UIImageView, so the whole imageView will be opaque and will respond to touches.
like image 407
iBug Avatar asked Jan 13 '16 06:01

iBug


People also ask

What is the difference between UIImage and UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage . Save this answer.

What is UIImageView in Swift?

A view that displays a single image or a sequence of animated images in your interface.

What is use of UIImageView?

Whenever you want an image in a view controller, you use a UIImageView. A UIImageView contains a UIImage, which is the raw bitmap, and allows you to configure how you want to scale or crop the image.

How do I change the background color in UIImageView?

- parameter color: The color of the background */ convenience init(withBackground color: UIColor) { let rect: CGRect = CGRect(x: 0, y: 0, width: 1, height: 1) UIGraphicsBeginImageContext(rect. size); let context:CGContext = UIGraphicsGetCurrentContext()!; context. setFillColor(color.


1 Answers

If both solution works for you the setting background colour would be better than setting image

[barImage setBackgroundColor:[UIColor blackColor]];
[greyBarImage setBackgroundColor:[UIColor greyColor]];

As this will

1.Save your app resources(Image).

2.Avoid runtime image loading issue(in case of rename replace).

Please refer Simple drawing app and Image drawing app.

like image 133
Avijit Nagare Avatar answered Sep 18 '22 00:09

Avijit Nagare