使用GCD异步下载图片,优化

本来在下载完成之后,在UIImageWriteToSavedPhotosAlbum 里面响应

image: (UIImage *) image didFinishSavingWithError: (NSError *) error  contextInfo: (void *) contextInfo

这个函数,然后来更新UI的,但是操作比较繁琐,而且UI的更新总是无法同步。所以采用GCD的方式,来完成,非常顺畅。

 

 

- (void)downloadImageToPhotoAlbum

{

    [self.viewaddSubview:downloadingView];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        NSURL *photoUrl = [NSURL URLWithString:self.photoVersionLargerUrl];

        NSData *photoData = [NSData dataWithContentsOfURL:photoUrl];

        UIImageWriteToSavedPhotosAlbum([UIImageimageWithData:photoData], nil, nil, nil);

        dispatch_async(dispatch_get_main_queue(), ^{

            [downloadingViewremoveFromSuperview];

        

        });

    });

}

 

- (void)processImage:(UIImage *)image

{

    [image retain];

    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

}

 

- (void)               image: (UIImage *) image

    didFinishSavingWithError: (NSError *) error

                 contextInfo: (void *) contextInfo

{

//    [downloadingView removeFromSuperview];

    NSLog(@"Save image complete!");

    if(error != nil) {

        NSLog(@"Error when saving image :%@",[error localizedDescription]);

    }

//    [image autorelease];

}

posted @ 2012-05-29 16:44  worldworld  阅读(502)  评论(0编辑  收藏  举报