2012-01-16 4 views
3

この質問は簡単すぎるかもしれません。カメラロールから画像を保存し直さないようにする方法

写真を撮るかカメラから選択するかをユーザーが選択できるようにするためのアクションシートがあります。

アプリはカメラのロールから選択されているかカメラから撮影されているかに関係なく、撮影した写真を保存します。何か不足していますか?

私の質問を読んでくれてありがとう。

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

答えて

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

あなたは

チェックこの機能今

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

} 

あなたがパスを指定する必要があり、このイメージを保存するには、このUIImage

を使用しUIImagePickerControllerDelegateを実装する必要が

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

あなたが編集オプションを与えていない場合は、UIImagePickerControllerOriginalImageを使用する代わりにUIImagePickerControllerEditedImage –

+1

しなければならない(picker.sourceType == UIImagePickerControllerSourceTypeCamera)場合の答え を見つけ { UIImageWriteToSavedPhotosAlbum(orginalImage、自己、@selector(imageSavedToPhotosAlbum:didFinishSavingWithError :contextInfo :)、nil); } – Desmond

関連する問題