嘿,我是 iPhone 新手,最近我一直在尝试制作一个应用程序。基本上,我想要做的是,如果用户将从相机捕获任何图像,那么它应该保存在设备库中。我知道如何将照片保存在图库中,它对我有用,但我无法将所有捕获的图像保存到设备图库中的特定文件夹(例如新相册)中。我希望用户能够拍摄几张照片,然后将图片从该特定文件夹复制到图库。
这是我的代码:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info valueForKey:UIImagePickerControllerMediaType];
if([mediaType isEqualToString:(NSString*)kUTTypeImage]) {
UIImage *photoTaken = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
self.imageView.image = photoTaken;
//Save Photo to library only if it wasnt already saved i.e. its just been taken
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
UIImageWriteToSavedPhotosAlbum(photoTaken, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
}
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
UIAlertView *alert;
if (error) {
alert = [[UIAlertView alloc] initWithTitle:@"Error!"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}
我尝试过执行下面的代码,但我不知道它将图像保存到我的 iPhone 设备中的应用程序资源中:
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"EMS_FOLDER"];
NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:@"Files"];
任何可供参考的来源或链接都值得赞赏。
谢谢您的帮助!
您可以使用AssetsLibrary,这将解决您的问题。
- (IBAction)takePhoto
{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.editing = YES;
imagePickerController.delegate = (id)self;
[self presentModalViewController:imagePickerController animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
[self.library saveImage:image toAlbum:@"Touch Code Magazine" withCompletionBlock:^(NSError *error) {
if (error!=nil) {
NSLog(@"Big error: %@", [error description]);
}
}];
[picker dismissModalViewControllerAnimated:NO];
}
这已经在这里解释了,一个很好的教程..
http://www.touch-code-magazine.com/ios5- saving-photos-in-custom-photo-album-category-for-download/ http://www.touch-code-magazine.com/ios5-saving-photos-in-custom-photo-album-category-for-download/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)