开发者

Downloading and writing images from an array of urls crashing iPad

I am writing images to the directory of my app using the following code in a separate thread

for (int j =0; j<[sorted count]; j++) {
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[sorted objectAtIndex:j]]];
    UIImage *image = [UIImage imageWithData:data];
    if (image!=nil) {
        NSLog(@"%@",[sorted obj开发者_如何学CectAtIndex:j]);
        [images addObject:image];
    }
}

and

for (int k=0;k<[images count];k++)
{
    NSString *temp = [[sorted objectAtIndex:k]lastPathComponent];
    NSString *imagePath = [dataPath stringByAppendingPathComponent:temp];
    NSData *data = UIImageJPEGRepresentation([images objectAtIndex:k], 1.0f);
    [data writeToFile:imagePath atomically:YES];

}

But a weird thing is last two images are not getting written I've tried everything but it doesn't seem to work. Anyone have any idea about this?


Not sure what could be causing your issue, but UIKit is not thread safe, so this could be the cause. You could try and execute your code on the main thread just to troubleshoot it (and check that it is correct), then, if my guess is right, look for a workaround.

In looking for a workaround, possibly performSelector:onMainThread: could help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜