Skip to content

Instantly share code, notes, and snippets.

@Pablo-Merino
Created May 15, 2011 11:18
Show Gist options
  • Save Pablo-Merino/973060 to your computer and use it in GitHub Desktop.
Save Pablo-Merino/973060 to your computer and use it in GitHub Desktop.
NSString *imageName =[NSString stringWithString:[(Tweet*)[tweets objectAtIndex:indexPath.row] author]];
NSString *userImage =[NSString stringWithString:[(Tweet*)[tweets objectAtIndex:indexPath.row] profileImage]];
NSArray *myArray = [userImage componentsSeparatedByString: @"/"];
NSString *fileName = (NSString*)[myArray lastObject];
NSString *picName =[NSString stringWithFormat:@"%@:%@", imageName, fileName];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/ProfilePictures"];
NSString* path = [dataPath stringByAppendingPathComponent:picName];
UIImage* image = [UIImage imageWithContentsOfFile:path];
if (!image) {
[activityIndicator startAnimating];
UIImage *phoneImage=[UIImage imageWithData:[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:userImage]]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/ProfilePictures"];
NSError *error;
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];
NSString* path = [dataPath stringByAppendingPathComponent:picName];
NSData* data = UIImagePNGRepresentation(phoneImage);
[data writeToFile:path atomically:YES];
}
else{
photo.image=image;
[activityIndicator stopAnimating];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment