只有在相机拍摄时才将图像保存到照片库,如果从照片库中选择则不能

时间:2014-02-17 00:36:54

标签: ios save photos

我的应用中有一个照片选择器,询问用户是否要拍照或从照片库中选择一个。完成他们的选择后,我将图像视图设置为他们选择的图像,我想保存他们刚刚设置的图像,如果它是从相机拍摄的,而不是它只是他们已经拥有的照片的编辑版本。这里列出的最后一种方法是我希望保存照片的方法。

- (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");
      }];
 }];

1 个答案:

答案 0 :(得分:3)

只需查看sourceType

即可
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
    // save to library
}

imagePickerController:didFinishPickingMediaWithInfo:方法中执行此操作。

相关问题