質問

In my iphone app I am downloading some number of images from the web. It doesn't matter if it blocks the UI thread, in fact it needs to block UI thread till fully downloaded. Once done, I notify the UI to wake up and display them.

My (simplified) code goes like this:

for (int i=0; i<10; i++)
{
    //call saveImageFromURL (params)
}
//Call to Notify UI to wake up and show the images

+(void) saveImageFromURL:(NSString *)fileURL :(NSString *)destPath :(NSString *)fileName
{
    NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];

    NSFileManager * fileManager = [NSFileManager defaultManager];

    BOOL bExists, isDir;
    bExists = [fileManager fileExistsAtPath:destPath isDirectory:&isDir];

    if (!bExists)
    {
        NSError *error = nil;
        [fileManager createDirectoryAtPath:destPath withIntermediateDirectories:YES attributes:nil error:&error];
        if (error)
        {
            NSLog(@"%@",[error description]);
            return;
        }
    }

    NSString *filePath = [destPath stringByAppendingPathComponent:fileName];
    [data writeToFile:filePath options:NSAtomicWrite error:nil];
}

When I am done with my for loop, I am pretty sure that all images are stored locally. And it works fine in simulator.

However it does not work well on my device. UI wakes up before images are stored. And almost all images seem empty.

What am I doing wrong?

役に立ちましたか?

解決 2

After some research, I used AFHttpClient enqueueBatchOfHTTPRequestOperations to accomplish multiple file downloads.

Here is how it goes:

//Consider I get destFilesArray filled with Dicts already with URLs and local paths

NSMutableArray * opArray = [NSMutableArray array];
AFHTTPClient *httpClient = nil;

for (id item in destFilesArray)
{
    NSDictionary * fileDetailDict = (NSDictionary *)item;
    NSString * url = [fileDetailDict objectForKey:@"fileURL"];
    if (!httpClient)
            httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:url]];

    NSString * filePath = [photoDetailDict objectForKey:@"filePath"];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];          

    operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:NO];
    [opArray addObject:operation];
}    

[httpClient enqueueBatchOfHTTPRequestOperations:opArray progressBlock:nil completionBlock:^(NSArray *operations)
{
    //gets called JUST ONCE when all operations complete with success or failure
    for (AFJSONRequestOperation *operation in operations)
    {

        if (operation.response.statusCode != 200)
        {                
            NSLog(@"operation: %@", operation.request.URL);
        }

    }

}];

他のヒント

  1. Check that if your device can download those images, visit the image URLs in Mobile Safari to test. dataWithContentsOfURL: will return nil OR it's not a correct image data, like 404 not found
  2. Log errors of [data writeToFile:filePath] to see the details of saving .
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top