0

As a newbie in iOS development (and programming), I am trying to accomplish something that I think is simple - but I need some help writing the code.

I have a mutable array that contains some facebook objects. The array is called facebookPhotosAllTemporary. I want to iterate through the array, pull out each object (a UIImage) at each index, and insert each of those images into a new array called facebookPhotosAll. Here is the code that I currently have:

In the implementation I have a private method:

- (void) addImagestoArray {
int i;
for (i = 0; i < [facebookPhotosAllTemporary count]; i++) {
    UIImage *image = [UIImage imageWithData:
                           [NSData dataWithContentsOfURL:
                            [NSURL URLWithString:
                             [[facebookPhotosAllTemporary objectAtIndex:i]
                              objectForKey:@"pic_big"]]]];
    [facebookPhotosAll addObject:image];
           }
}

Then in the viewDidLoad section I call the method:

[self addImagestoArray];

When I run it, my NSLog message says there are no objects in the array I am trying to add objects to.

I realize my code could be completely flawed! But could someone put me on the right track? Thanks!

1 Answer 1

2

There are a few issues here.

  1. Did you allocate and initialize facebookPhotosAll? In other words, somewhere, such as in your init method or in viewDidLoad, did you do something like:

    facebookPhotosAll = [[NSMutableArray alloc] init];

  2. You are loading a set of images from the Internet synchronously on the main thread of your app. This will hang the user interface. Best case is this will make your app look frozen briefly. Worst case the OS will kill your app for failing to respond if this takes too long.

  3. The line you wrote to get the image is nasty, hard to read, and difficult to debug. Split that into 5 lines. This way you can see each part in case there are issues.

To deal with issue 2 you should refer to the LazyTableImages sample app that Apple provides. This shows the proper way to load images in the background. Never do synchronous Internet access on the main thread.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.