我的应用中有一个照片选择器,询问用户是否要拍照或从照片库中选择一个。完成他们的选择后,我将图像视图设置为他们选择的图像,我想保存他们刚刚设置的图像,如果它是从相机拍摄的,而不是它只是他们已经拥有的照片的编辑版本。这里列出的最后一种方法是我希望保存照片的方法。
- (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");
}];
}];
答案 0 :(得分:3)
只需查看sourceType
:
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
// save to library
}
在imagePickerController:didFinishPickingMediaWithInfo:
方法中执行此操作。