簡體   English   中英

只有在相機拍攝時才將圖像保存到照片庫,如果從照片庫中選擇則不能

[英]Save images to photo library ONLY if they are taken by camera and not if chosen from photo library

我的應用程序中有一個照片選擇器,詢問用戶是否要拍照或從照片庫中選擇一個。 完成他們的選擇后,我將圖像視圖設置為他們選擇的圖像,我想保存他們剛剛設置的圖像,如果它是從相機拍攝的,而不是它只是他們已經擁有的照片的編輯版本。 這里列出的最后一個方法是我希望保存照片的方法。

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

只需檢查sourceType

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

imagePickerController:didFinishPickingMediaWithInfo:執行此imagePickerController:didFinishPickingMediaWithInfo:方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM