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

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

我有一个操作表,允许用户select如何拍摄照片或从相机中select。

该应用程序将保存用户拍摄的照片,无论是从相机卷select或从相机拍摄。 我错过了什么吗?

感谢您阅读我的问题。

#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]; } 

试试这个代码..它从我身边正常工作。

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

你需要实现UIImagePickerControllerDelegate

检查这个function

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

现在使用这个UIImage

要保存此图像您必须指定一个path

 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];