仅当它们由相机拍摄而不是从照片库中选择时才将图像保存到照片库
Posted
技术标签:
【中文标题】仅当它们由相机拍摄而不是从照片库中选择时才将图像保存到照片库【英文标题】:Save images to photo library ONLY if they are taken by camera and not if chosen from photo library 【发布时间】:2014-02-17 00:36:53 【问题描述】:我的应用中有一个照片选择器,它会询问用户是要拍照还是从照片库中选择一张。完成他们的选择后,我为他们选择的图像设置了一个图像视图,我想保存他们刚刚设置的图像,前提是它是从相机拍摄的,而不是他们已经拥有的照片的编辑版本。此处列出的最后一种方法是我希望保存照片的方法。
- (IBAction)startPicker:(id)sender
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
UIActionSheet *picChoice = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Camera", @"Photo Library", nil];
[picChoice showFromRect:[(UIButton *)sender frame] inView:self.view animated:YES];
else
UIActionSheet *picChoice = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Photo Library", nil];
[picChoice showFromRect:[(UIButton *)sender frame] inView:self.view animated:YES];
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:@"Camera"])
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
else if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:@"Photo Library"])
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
else
return;
[self presentViewController:imagePicker animated:YES completion:^
];
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
[library writeImageToSavedPhotosAlbum:editedImage.CGImage orientation:(ALAssetOrientation)editedImage.imageOrientation completionBlock:^(NSURL *assetURL, NSError *error )
NSLog(@"IMAGE SAVED TO PHOTO ALBUM");
[library assetForURL:assetURL resultBlock:^(ALAsset *asset )
NSLog(@"we have our ALAsset!");
failureBlock:^(NSError *error )
NSLog(@"Error loading asset");
];
];
【问题讨论】:
how to prevent saving picture from camera roll again的可能重复 仅供参考 - 我通常不会发布对我也标记为重复的问题的答案,但重复的问题是如此混乱,以至于答案不太明显。 【参考方案1】:只需检查sourceType
:
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera)
// save to library
在您的 imagePickerController:didFinishPickingMediaWithInfo:
方法中执行此操作。
【讨论】:
以上是关于仅当它们由相机拍摄而不是从照片库中选择时才将图像保存到照片库的主要内容,如果未能解决你的问题,请参考以下文章