如何防止再次从相机胶卷保存图片

3
也许这个问题太简单了,或者我不知道它有多简单。
我有一个 actionsheet,允许用户选择他们是否要拍照或从相机中选择。
无论是从相机胶卷中选择还是从相机中拍摄,应用程序都会保存用户拍摄的照片。我有什么遗漏吗?
感谢您阅读我的问题。
#pragma mark - photo select

-(IBAction)showCameraAction:(id)sender
{
    if(![UIImagePickerController isSourceTypeAvailable:
         UIImagePickerControllerSourceTypeCamera])
    {
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@""
                                                                 delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil
                                                        otherButtonTitles:@"Choose Photo From Library", nil];
        actionSheet.actionSheetStyle = UIActionSheetStyleAutomatic;
        actionSheet.tag =1;
        [actionSheet showInView:[self.view superview]]; 

    }
    else
    {
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@""
                                                                 delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil
                                                        otherButtonTitles:@"Take Photo With Camera", @"Choose Photo From Library", nil];
        actionSheet.actionSheetStyle = UIActionSheetStyleAutomatic;
        actionSheet.tag =1;
        [actionSheet showInView:[self.view superview]]; 
    }
}


- (void)getPhotoFromSource:(UIImagePickerControllerSourceType)sourceType;
{
    //NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:sourceType];
    if ([UIImagePickerController isSourceTypeAvailable:sourceType]) 
    {
        //NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:sourceType];
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        //picker.mediaTypes = mediaTypes;
        picker.delegate = self;
        //picker.allowsEditing = YES;
        picker.sourceType = sourceType;
        [self presentModalViewController:picker animated:YES];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error accessing media" message:@"Device doesn't support media source" delegate:nil cancelButtonTitle:@"Drat" otherButtonTitles:nil, nil];
        [alert show];
    }
}

#pragma mark UIImagePickerController delegate methods
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    self.lastChosenMediaType = [info objectForKey:UIImagePickerControllerOriginalImage];

    imageFrame=fImageView.frame;

        //UIImage *chosenImage = [info objectForKey:UIImagePickerControllerEditedImage];
        UIImage *orginalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
        UIImage *shrunkenImage = shrinkImage(orginalImage,imageFrame.size);
        self.fImage = shrunkenImage;

    fImageView.image = frogImage;

//answer fix, to prevent saving picture from cameraroll again.        
 if (picker.sourceType == UIImagePickerControllerSourceTypeCamera)
       {
        UIImageWriteToSavedPhotosAlbum(orginalImage, self, @selector(imageSavedToPhotosAlbum: didFinishSavingWithError: contextInfo:), nil);  
       }

    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    //NSString *message;
    //NSString *title;

//  if (!error) {
//      title = NSLocalizedString(@"SaveSuccessTitle", @"");
//      message = NSLocalizedString(@"SaveSuccessMessage", @"");
//  } else {
//      title = NSLocalizedString(@"SaveFailedTitle", @"");
//      message = [error description];
//  }
//  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
//                                                  message:message
//                                                 delegate:nil
//                                        cancelButtonTitle:NSLocalizedString(@"ButtonOK", @"")
//                                        otherButtonTitles:nil];
//  [alert show];
}
2个回答

1

你需要实现UIImagePickerControllerDelegate

检查这个函数

  • (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{}
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType]; 
if ([mediaType isEqualToString:@"public.image"]){
        UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];

}

现在使用这个UIImage

要保存这个图像,您必须指定一个路径

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"photo.png"]];

//COnvert it to NSData before saving and then save it
    NSData *webData = UIImagePNGRepresentation(editedImage);
    [webData writeToFile:imagePath atomically:YES];

如果您不提供编辑选项,则必须使用UIImagePickerControllerOriginalImage而不是UIImagePickerControllerEditedImage。 - Virat Naithani
1
找到了答案 如果(picker.sourceType == UIImagePickerControllerSourceTypeCamera) { UIImageWriteToSavedPhotosAlbum(orginalImage, self, @selector(imageSavedToPhotosAlbum: didFinishSavingWithError: contextInfo:), nil);
}
- Desmond

1

尝试这段代码。从我的角度来看,它可以正常工作。

- (IBAction) uploadPhoto:(id)sender
{
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@""
                                                                 delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil
                                                        otherButtonTitles:@"Use Photo from Library", @"Take Photo with Camera", nil];
        actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
        actionSheetAction = ActionSheetToSelectTypeOfSource;
        [actionSheet showInView:self.view];
        [actionSheet release];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    UIImagePickerControllerSourceType sourceType;
                if (buttonIndex == 0) {
                    sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                } else if(buttonIndex == 1) {
                    sourceType = UIImagePickerControllerSourceTypeCamera;
                }else {
                    // Cancel
                    break;
                }
                if([UIImagePickerController isSourceTypeAvailable:sourceType]) {
                    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
                    picker.sourceType = sourceType;
                    picker.delegate = self;
                    if (sourceType == UIImagePickerControllerSourceTypeCamera) {
                        picker.cameraFlashMode = UIImagePickerControllerCameraFlashModeOn;
                    }
                    picker.allowsImageEditing = NO;
                    [self presentModalViewController:picker animated:YES];
                    [picker release];
                }

}

#pragma mark -
#pragma mark UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
   UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES);
 NSString *docDirectory = [paths objectAtIndex:0];
 NSString *imgPath = [documentsDirectory stringByAppendingPathComponent:[NSString  stringWithFormat:@"image1.png"]];

//COnvert it to NSData before saving and then save it
    NSData *imgData = UIImagePNGRepresentation(image);
    [imgData writeToFile:imgPath atomically:YES];
   [self dismissModalViewControllerAnimated:YES];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self dismissModalViewControllerAnimated:YES];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接