IOS How to save and get iamge from url
/* save image from url */
+ (void) saveImageFromURL:(NSString *) folder file:(NSString *)fileName url:(NSString*)fileURL {
NSString *filePath;
[self removeFileFromDocuments:folder file:fileName];
NSString* documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
if([folder length] == 0) {
filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
} else {
filePath = [[[[documentsDirectory stringByAppendingString:@"/"] stringByAppendingString:folder] stringByAppendingString:@"/"] stringByAppendingString:fileName];
}
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:filePath]) {
// Get an image from the URL below
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]]];
NSData *data = [NSData dataWithData:UIImagePNGRepresentation(image)];
[data writeToFile:filePath atomically:YES];
[image release];
}
}
/* get image from url */
+ (UIImage*) getImageFromURL:(NSString*)fileURL {
// Get an image from the URL below
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]]];
return [image autorelease];
}
+ (UIImage*) getSynchronousImage:(NSString*)fileURL {
NSError * err;
NSURLResponse * response;
@autoreleasepool {
NSString *strURL = [fileURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:strURL]];
NSData * receivedData = [NSURLConnection sendSynchronousRequest:imageRequest
returningResponse:&response
error:&err];
if( !receivedData ){
/* Handle error */
UtiLog(@"Error:%@",[err description]);
}
UIImage *image = [[UIImage alloc] initWithData:receivedData];
return image;
}
}
+ (void) saveImageFromURL:(NSString *) folder file:(NSString *)fileName url:(NSString*)fileURL {
NSString *filePath;
[self removeFileFromDocuments:folder file:fileName];
NSString* documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
if([folder length] == 0) {
filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
} else {
filePath = [[[[documentsDirectory stringByAppendingString:@"/"] stringByAppendingString:folder] stringByAppendingString:@"/"] stringByAppendingString:fileName];
}
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:filePath]) {
// Get an image from the URL below
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]]];
NSData *data = [NSData dataWithData:UIImagePNGRepresentation(image)];
[data writeToFile:filePath atomically:YES];
[image release];
}
}
/* get image from url */
+ (UIImage*) getImageFromURL:(NSString*)fileURL {
// Get an image from the URL below
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]]];
return [image autorelease];
}
+ (UIImage*) getSynchronousImage:(NSString*)fileURL {
NSError * err;
NSURLResponse * response;
@autoreleasepool {
NSString *strURL = [fileURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:strURL]];
NSData * receivedData = [NSURLConnection sendSynchronousRequest:imageRequest
returningResponse:&response
error:&err];
if( !receivedData ){
/* Handle error */
UtiLog(@"Error:%@",[err description]);
}
UIImage *image = [[UIImage alloc] initWithData:receivedData];
return image;
}
}
Comments
Post a Comment