0

I am trying to save an image in an NSMutable array and it is not working

here is what I am doing

[imagesList addObject:[UIImage imageNamed:@"b.png"]];

after executing this line I noticed that the number of objects remains 0 any reason ?

Thanks

I repeate this code in several areas :

Globally I declare :

NSMutableArray *imagesList;
NSUserDefaults *imagesHistory;

in my viewdidload method I write:

imagesHistory=[NSUserDefaults standardUserDefaults];  //imagesHistory is created globallt as NSUserDefault imagesHistory
[imagesHistory setObject:imagesList forKey:@"images history"];

UIImage *image;
image=[UIImage imageNamed:@"b.png"];

[imagesList addObject:image];
imagesHistory=[NSUserDefaults standardUserDefaults];
[imagesHistory setObject:imagesList forKey:@"images history"];

and in the didFinishLaunchingWithOptions I write : (even though I don t need to do it when I am adding strings ...)

 imagesList = [[NSMutableArray alloc] init];
Shaggy Frog
  • 27,575
  • 16
  • 91
  • 128
user1415780
  • 1,153
  • 5
  • 16
  • 33
  • 2
    Have you tried making the image on a separate line, and seeing if it is `nil` or not? Are you sure `imagesList` has been allocated and initialized properly? – Dan F May 24 '12 at 18:23
  • 1
    Either the image can't be loaded (there is no b.png) or imagesList isn't actually mutable. – DrummerB May 24 '12 at 18:23
  • @DanF : I made it on two lines ( I creaded a UIImage variable assigned the image to it and I can see that the memory was allocated to this variable) and then I added it to the NSMUtablearray and still the object wasn t added . imagesList is a global variable and from experience I do t need to init and alloc it , or am I missing something? – user1415780 May 24 '12 at 18:42
  • @DrummerB: the image exists and imagesList is Mutable – user1415780 May 24 '12 at 18:43
  • Of course you need to alloc and init it. That's your problem then. – DrummerB May 24 '12 at 18:45
  • @DrummerB : look at my comments below – user1415780 May 24 '12 at 19:57

4 Answers4

3

Regardless of whether it is a global variable or not, you still need to call alloc and init SOMEWHERE for the object. If you intend to use it throughout your app, then appDidFinishLaunchingWithOptions is a decent place to add this call:

imagesList = [[NSMutableArray alloc] init];
Dan F
  • 17,654
  • 5
  • 72
  • 110
  • Do you have some more lines of code to show, around where you're adding the object for us to see how you're determining the number of objects in the array? Also, the declaration of your `imagesList` is helpful information – Dan F May 24 '12 at 19:59
  • What exactly are you doing to determine that there are 0 objects in the imagesList? – Dan F May 24 '12 at 20:53
1

Is your empty array being retained? If you're not using Automatic Reference Counting, there's a good chance you're initializing the array with the following

imagesList = [[NSMutableArray alloc] init];

but it's not being retained. You'll want to retain the empty array so it gets appended to further on in your code.

imagesList = [[[NSMutableArray alloc] init] retain];

Just don't forget to release the array when you're all done with it, in viewDidUnload or wherever is appropriate.

Josh Hudnall
  • 1,021
  • 8
  • 16
0

You're allocating/initing imagesList AFTER you try to add an object. You need to alloc/init imageList before you add anything to it.

To make sure it's there, try something like this:

if (!imagelist) imageList = [[NSMutableArray alloc] init];

If it exists, and you're still having this problem, it's possible that you're allocating/initing a new NSMutableArray and assigning it to imageList after you've added the object. This means the old array would be discarded and you'd be left with a new array with zero items.

Aaron Hayman
  • 8,492
  • 2
  • 36
  • 63
  • I followed your advice and tried adding this line in the app delegate and also just before adding the object and still encountering the same issue ... – user1415780 May 24 '12 at 20:27
  • Is it possible you're allocating/initing too many time? See my updated answer. – Aaron Hayman May 24 '12 at 20:33
  • I checked and it I only allocat the variable one . I also made a small test and found out that if I change the image in the addobject to a string , it works perfectly fine ... – user1415780 May 24 '12 at 21:32
  • It sounds like there's a problem in the image itself then. Maybe it's not actually a png or perhaps it didn't loaded correctly into Xcode. I'd remove it from Xcode, try to load it back in. Also, make sure the file name matches perfectly, including caps. A stray capital letter will prevent it from loading on devices (though I think the simulator will still load it). If none of that works, maybe recreate the image? I don't know how difficult that would be but that's the last thing I can think of. – Aaron Hayman May 25 '12 at 12:28
  • I tried to add another image that I am actually using .. still the same problem – user1415780 May 25 '12 at 14:21
  • In all honesty, I'm not sure. UIImage should return `nil` if it can't find the image. You'll get an error if you try to add `nil` to a NSMutableArray. The only thing I can think of is there is another part of your code removing the image or replacing the array with a new one. – Aaron Hayman May 25 '12 at 15:35
0

try UIImageView *image; image=[UIImage imageNamed:@"b.png"];

also, in @property(nonatomic,retain) use 'strong' instead of 'retain'

Breakpoint
  • 1,511
  • 1
  • 18
  • 41