3

i have an ipad app (>30 views / pages) each view has a unique background.

the problem: whats the best way to set the background (memory friendly)

is there a better way than adding: uiimageview "backgroundView" as a subview?

version1:

[[UIImage alloc] initWithData:imageData];

which seems to be problematic with the retina switch

version2:

self.layer.contents = (id)image.CGImage;

version 3:

UIImage* image = [UIImage imageWithContentsOfFile:fileLocation];

version 2 seems to work fine. maybe someone tell me whats the best approach, and why ;)

thank you Alex

Alex Milde
  • 511
  • 1
  • 7
  • 21

2 Answers2

1

CGImage is problematic with retina ... version3. is best for memory friendly !

Dream.In.Code
  • 399
  • 3
  • 5
0

In version 2, you generate a new image object which you have to release manually if you don't use ARC. Version 3 uses an autoreleased object.

Both versions are equal in memory-friendlyness. I'd prefer version 3 because you don't have to do anything yourself to free the memory.

You could also use [UIImage imageNamed:@"image-name.png"], which also generates an autoreleased object.

If you want it as memory-friendly as possible, you should consider using PVR images, as those are natively supported by the graphics hardware.

Best,

Flo

guitarflow
  • 2,930
  • 25
  • 38
  • hi, thanks for the quick response. but imagenamend will eat up to much memory ;/ so i would need something like [self addSbview:UIIMAGEVIEW] aswell? cause i wouldnt neet this in version 2? – Alex Milde May 14 '12 at 14:53
  • Why is imageNamed too memory-consuming? I couldn't think of a reason why this constructor would create a bigger memory block than the other. Maybe I don't get your question right, but in either way you will have to load an image and assign it to either an image view or a CoreAnimation layer. I don't think that using an UIImageView creates that much of an overhead. – guitarflow May 14 '12 at 15:37
  • because the image is cached by the app. just google "imagenamed - memory leak" well - actually it does create that much overhead on bigger apps. – Alex Milde May 14 '12 at 15:48