2014-04-08 37 views
0

我有一個按鈕來捕獲圖片,然後保存到photoAlbum。我用另一個按鈕來加載圖像使用imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;然後我實現了didFinishPickingMediaWithInfo:加載相冊到newImage。我的需要是捕捉照片後,我有alertview,然後單擊是按鈕自動加載上次捕獲的圖像PhotoAlbum並添加newImage。可能嗎?如何從相冊加載捕獲的圖像

- (void)saveImageToPhotoAlbum 
{ 

    [self.library saveImage:[self stillImage] toAlbum:@"Album" withCompletionBlock:^(NSError *error) { 
     if (error!=nil) { 
      NSLog(@"Big error: %@", [error description]); 
     } 
    }]; 


    UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Gallery" 
                 message:@"Do you want to use the captured image?" delegate:self 
               cancelButtonTitle:@"Cancel" otherButtonTitles:@"Yes", nil]; 


    alertView.tag = 2; 
    [alertView show]; 


} 

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ 
    //set image 


    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 



     newImage = [[UIImageView alloc] initWithImage:[info objectForKey:UIImagePickerControllerOriginalImage]]; 

     [newImage setFrame:CGRectMake(0, 0, 320, 568)]; 

     [self.view addSubView:newImage]; 
} 

我需要最後拍攝的圖像顯示於alertView按鈕的點擊newImage:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    // the user clicked one of the OK/Cancel buttons 

    if (buttonIndex == 1){ 

} 
} 

回答

0
Before use below code take UIImage *img in .h file 

    - (void)saveImageToPhotoAlbum 
     { 

      [self.library saveImage:[self stillImage] toAlbum:@"Album" withCompletionBlock:^(NSError *error) { 
       if (error!=nil) { 
        NSLog(@"Big error: %@", [error description]); 
       } 
      }]; 


      UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Gallery" 
                   message:@"Do you want to use the captured image?" delegate:self 
                 cancelButtonTitle:@"Cancel" otherButtonTitles:@"Yes", nil]; 


      alertView.tag = 2; 
      [alertView show]; 


     } 

     -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ 
      //set image 
     img=nil; 
     img=[info objectForKey:UIImagePickerControllerOriginalImage]; 
     [self saveImageToPhotoAlbum]; 
//  [self dismissViewControllerAnimated:YES completion:nil]; 
    } 

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
    { 

     if (buttonIndex == 1) 
     { 

      if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 

         if(newImage) 
         { 
          [newImage removeFromSuperview]; 
           newImage=nil; 
         } 
         newImage = [[UIImageView alloc] initWithImage:img]; 

         [newImage setFrame:CGRectMake(0, 0, 320, 568)]; 

         [self.view addSubView:newImage]; 
      } 
      [picker dismissViewControllerAnimated:YES completion:nil]; 
     } 
     else 
     { 
      img=nil; 
      [picker dismissViewControllerAnimated:YES completion:nil]; 
     } 
    } 
+0

未聲明varaiable拾取器的位置:[拾取器dismissViewControllerAnimated:YES完成:無]; – user3496826

+0

它不起作用 – user3496826

+0

在.h文件中聲明UIImagePickerController * myPicker。 –

相關問題