Reputation: 667
I am using photo library in my app. And after selecting image from photo library or camera i want to save that image into my Documents folder. So here i want to give name to that image while saving is it possible to set name to the selected image? If means please let me know. I am trying that only if possible i will post that.Thank you.
Upvotes: 1
Views: 391
Reputation: 1403
Declare an integer counterNo and initialize it to 0. And increase its value to change name dynamically.
UIImage *photoImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
counterNo++;
NSString *aaa = [NSString stringWithFormat:@"Documents/myImage-%d.png" , counterNo];
NSString *savedDoc = [NSHomeDirectory() stringByAppendingPathComponent:aaa];
BOOL status = [[NSFileManager defaultManager] fileExistsAtPath:savedDoc];
if(status){
[[NSFileManager defaultManager] removeItemAtPath:savedDoc error:nil];
}
[UIImagePNGRepresentation(photoImage) writeToFile:savedDoc atomically:YES];
[picker dismissModalViewControllerAnimated:YES];
Upvotes: 0
Reputation: 3294
This is how I do it.. I'm copying and pasting my code so there's some additional functionality.
// image is a UIImage
// inputText is the user selected imagename
// date is a string I inserting to make pictures a unique identifier (i.e. no duplicate names)
NSData *imageData1 = UIImageJPEGRepresentation(image, 1.0);
NSString *imageFilename = [NSString stringWithFormat:@"%@-%@.jpg", inputText,date];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [NSString stringWithFormat:@"%@/%@", [paths objectAtIndex:0], imageFilename];
if([imageData1 writeToFile:path atomically:YES]){
NSLog(@"Write to Document folder success filename = %@",path);
}
Upvotes: 1
Reputation: 1403
Use following :
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *photoImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSString *savedDoc = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/myImageName.png"];
BOOL status = [[NSFileManager defaultManager] fileExistsAtPath:savedDoc];
if(status){
[[NSFileManager defaultManager] removeItemAtPath:savedDoc error:nil];
}
[UIImagePNGRepresentation(photoImage) writeToFile:savedDoc atomically:YES];
[picker dismissModalViewControllerAnimated:YES];
}
You can set any desired name when saving to document directory.
Upvotes: 0