0

i have two image:

-map.png (1985 x 1162)(about 200kb)

-map@2x.png (3969 x 2324) (about 650kb)

now if load this image in this way:

imageMap = [UIImage imageNamed:@"map.png"];

viewMap = [[UIView alloc] initWithFrame:CGRectMake(0, 0, imageMap.size.width, imageMap.size.height)];
viewMap.center = CGPointMake(self.view.frame.size.height/2, self.view.frame.size.width/2);
viewMap.backgroundColor = [UIColor colorWithPatternImage:imageMap];

[self.view addSubview:viewMap];

the image does not load completely in iPhone4 (but works well in iPhone5) and I do not know why.

But if i put in bundle only

map@2x.png

and rename it in

map.png

using the same code, the image is loaded without problems.

what happens? thanks everybody

EDIT:

I think the problem comes from setting the background of a UIView because if I modify the code like this:

imageMap = [UIImage imageNamed:@"map.png"];

viewMap = [[UIView alloc] initWithFrame:CGRectMake(0, 0, imageMap.size.width, imageMap.size.height)];
viewMap.center = CGPointMake(self.view.frame.size.height/2, self.view.frame.size.width/2);
//  viewMap.backgroundColor = [UIColor colorWithPatternImage:imageMap];

[self.view addSubview:viewMap];

UIImageView *imageView = [[UIImageView alloc] initWithImage:imageMap];
[viewMap addSubview:imageView];

you can see the image correctly.

Ilario
  • 5,979
  • 2
  • 32
  • 46
  • You don't need the `.png` when using [`+[UIImage imageNamed:]`](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImage_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006890-CH3-SW10) anymore btw. – Rich Apr 24 '14 at 10:19
  • Check if @2x image is actually copied to app bundle – Vladimir Apr 24 '14 at 10:24
  • @Vladimir yes there is in bundle! because with iphone5 i see the image! now i suppose that the problem is that is too big for put this image as background of UIView – Ilario Apr 24 '14 at 10:27
  • try to use it like this http://stackoverflow.com/a/8077854 – TonyMkenu Apr 24 '14 at 12:46
  • or…. viewMap.layer.contents = (id)[UIImage imageNamed:@“map.png"].CGImage – TonyMkenu Apr 24 '14 at 12:48

1 Answers1

2

Your images are probably too big. You say you have

-map.png (1985 x 1162) (about 200kb)

-map@2x.png (3969 x 2324) (about 650kb)

The size of an image file in bytes, however, is hardly representative of its size in memory. To the best of my knowledge, loading an image of AxB pixels results in somewhere in the neighborhood of A * B * 4 bytes memory usage. In your case, that's about 23MB for the smaller image and 90MB for the bigger one.

Considering that the iPhone 4 has a whopping 512MB of RAM and the iPhone 5 1GB and that apps are limited in how much of that RAM they are entitled to, you might want to rethink how big you want to make these images.

If you are doing something like a map, tiling would probably work. See CATiledLayer.

Community
  • 1
  • 1
dandan78
  • 13,328
  • 13
  • 64
  • 78
  • thank you! have any direct example of how to use CATiledLayer? – Ilario Apr 24 '14 at 13:16
  • Unfortunately I do not, but I'm sure there are numerous examples online. – dandan78 Apr 24 '14 at 13:18
  • i'm trying with load .svg in webview, but i have some problem. If you want check this: http://stackoverflow.com/questions/23274383/add-uibutton-as-subview-of-uiwebview-and-resize-it-when-zoom thanks – Ilario Apr 24 '14 at 16:16