0

I'm using the AFNetworking API and I'm using the AFImageRequestOperation function to call a Image URL from a server API to convert that into a UIImage. What I want to do is; every time I call this method:

NSURLRequest *requestImage = [NSURLRequest requestWithURL:theImageURL];
// theImageURL is the Image URL that is collected in a previous method from the server.

            AFImageRequestOperation *imageOperation = [AFImageRequestOperation imageRequestOperationWithRequest:requestImage imageProcessingBlock:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
            {
                 objImageArray = [[NSMutableArray alloc] init];
                 if (requestImage != nil) {

                 [objImageArray addObjectsFromArray:image];

                 }


            } failure:nil];

            [imageOperation start];

So I have called the URL and successfully assigned the image URL to the UIImage. I've then allocated a NSMutableArray and added the image to it. The problem I'm having is that every time the method is called again the MutableArray replaces the old Image with the new one. I Thanks In advance!

Ollie177
  • 315
  • 1
  • 3
  • 17

2 Answers2

1

You should be able to set the NSMutableArray in the .h file.

@property(nonatomic, strong) NSMutableArray * objImageArray;

Then remove the code where you create the array in your .m file.

objImageArray = [[NSMutableArray alloc] init];

Then you can use the same add object but you need to make sure that you are setting it to the array.

[self.objImageArray addObjectsFromArray:image];

EDIT: The array doesn't care about the names of the object it contains so they can all have the same name. When you are looking fo the images you after getting the image from the array index not the name.

Hope this helps.

Will
  • 3,004
  • 29
  • 43
0

Every time this block is called you recreate an objImageArray. Thus it becomes empty, old UIImage autorelease pool releases old UIImage and then you add a new UIImage => not more than 1 object at any time. You should create it once, not every time the block is called. At least, try to allocate it outside the block.

efpies
  • 3,625
  • 6
  • 34
  • 45
  • Argh, totally should have seen this... Thanks for pointing out my mistake - I made the changes and it now adds the UIImage instead of replacing it. I should be able to get it to work from here on out. Sometimes it takes an outside eye to spot what I'm doing wrong! Thanks for your help. – Ollie177 Nov 14 '12 at 11:22