Descargar imagen asíncronamente
- (void)downloadImageWithURL:(NSURL *)url completionBlock:(void (^)(BOOL succeeded, UIImage *image))completionBlock
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if ( !error )
{
UIImage *image = [[UIImage alloc] initWithData:data];
completionBlock(YES,image);
} else{
completionBlock(NO,nil);
}
}];
}
- (NSString *) saveImage2:(NSString *)stringURL :(NSString *)name{
NSLog(@"--- %s ---", __PRETTY_FUNCTION__);
__block NSString *filePath = [[NSString alloc] init];
__block NSData *storeImageData;
[self downloadImageWithURL:[NSURL URLWithString:stringURL] completionBlock:^(BOOL succeeded, UIImage *image) {
if (succeeded) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,[NSString stringWithFormat:@"%@.png", name]];
storeImageData = UIImagePNGRepresentation(image);
[storeImageData writeToFile:filePath atomically:YES];
}
}];
return filePath;
}
Comentarios
Publicar un comentario