简体   繁体   中英

How to save and retrieve photos in photo album with their names

I have the following doubts:

  1. Can we save a photo(image) to photo album with a "specific name" or can we access the name of the photo after saving it?

  2. Can we access a photo with its name from the photo album?

Please help.

Thanks in advance.

This is the code for writing images with user specified name

 // Create paths to output images
 NSString  *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.png"];
 NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.jpg"];

 // Write a UIImage to JPEG with minimum compression (best quality)
 // The value 'image' must be a UIImage object
 // The value '1.0' represents image compression quality as value from 0.0 to 1.0
 [UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES];

 // Write image to PNG
 [UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];

 // Let's check to see if files were successfully written...

 // Create file manager
 NSError *error;
 NSFileManager *fileMgr = [NSFileManager defaultManager];

 // Point to Document directory
 NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

 // Write out the contents of home directory to console
 NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);

Using delegate UIImagePickerControllerDelegate for invoking camera or image library.

To Invoke Camera:

UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;

picker.sourceType = UIImagePickerControllerSourceTypeCamera;
isCamera = YES;

[self presentModalViewController:picker animated:YES];

To Invoke Library:

UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;

[picker setAllowsEditing:YES];

[self presentModalViewController:(UIViewController*)picker animated:YES];

Once done, delegate method gets called:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

To write image to library use:

UIImageWriteToSavedPhotosAlbum(currentImage, nil, nil, nil);

or save selected image picked from photo library to UIImage and perform further actions on it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM