如何用他们的名字保存和检索相册中的照片
Posted
技术标签:
【中文标题】如何用他们的名字保存和检索相册中的照片【英文标题】:How to save and retrieve photos in photo album with their names 【发布时间】:2011-06-27 07:10:41 【问题描述】:我有以下疑惑:
我们可以将照片(图像)保存到“特定名称”的相册中,还是可以在保存后访问照片的名称?
我们可以访问相册中带有照片名称的照片吗?
请帮忙。
提前致谢。
【问题讨论】:
【参考方案1】:这是编写用户指定名称的图像的代码
// 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]);
【讨论】:
感谢您回答我的问题。实际上,我需要将照片保存到具有特定名称的相册,而不是文档目录。【参考方案2】:使用委托UIImagePickerControllerDelegate
调用相机或图像库。
调用相机:
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
isCamera = YES;
[self presentModalViewController:picker animated:YES];
调用库:
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
[picker setAllowsEditing:YES];
[self presentModalViewController:(UIViewController*)picker animated:YES];
一旦完成,委托方法就会被调用:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
将图像写入库使用:
UIImageWriteToSavedPhotosAlbum(currentImage, nil, nil, nil);
或将从照片库中选择的图像保存到UIImage
并对其执行进一步的操作。
【讨论】:
@JaninAnk:嗨,我知道如何使用 imagePicker 和保存图像等。但我的疑问是,如何保存和访问具有特定名称的图像。 你的程序不允许在它的“沙盒”之外保存东西。以上是关于如何用他们的名字保存和检索相册中的照片的主要内容,如果未能解决你的问题,请参考以下文章