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

时间:2012-01-16 07:05:42

标签: iphone ios xcode uiimagepickercontroller

可能这个问题太容易了,或者我不知道它会有多容易。

我有一个操作表,允许用户选择拍照或从相机中选择的方式。

该应用程序将保存用户拍摄的照片,无论是从相机胶卷中选择还是从相机中取出。我错过了什么吗?

感谢您阅读我的问题。

#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 个答案:

答案 0 :(得分: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];

答案 1 :(得分: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];
}