iDev: Saving and Getting Image from NSDocumentDirectory (get image from URL)

Look the Code”

//Getting the image from URL

-(void)savedPhotoInDocDir:(NSString *) imageURL{

NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]];

NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];

//TimeStamp getting random number
int randomNumber = [self getRandomNumber:9 to:9999999999];

//Creating Time stamp for specific name of save Image

NSString * timeStampValue = [NSString stringWithFormat:@”%ld”, (long)[[NSDate date] timeIntervalSince1970]];

[self setImagePath:[documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@”savePhoto%@%d.png”,timeStampValue,randomNumber]]];

BOOL isDone =[fileManager createFileAtPath:imagePath contents:imageData attributes:nil];

if(isDone==TRUE){
NSLog(@”Saved Image Document Directory”); }
else {
NSLog(@”Not Save”);
}
}

//Generating Random number

-(int)getRandomNumber:(int)from to:(int)to {

return (int)from + arc4random() % (to-from+1);
}

// getting the image

– (UIImage*)getImage_FromName :(NSString *)imageName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:imageName];
UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];
return img;
}

Keep Coding …

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s