2014-05-10 112 views
0

我已經編寫這個程序來保存圖像,同時採摘的UIImage,但我需要的代碼 保存UIImagePickerControllerSourceTypePhotoLibrary圖像,而只能通過相機捕捉
圖像。保存拍攝的圖像在照片庫中

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
if([info objectForKey:@"UIImagePickerControllerOriginalImage"]) 
{ 
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType]; 
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) 
    { 
     UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; 
     UIImageWriteToSavedPhotosAlbum(image,nil,nil,nil); 
     PhotoViewController *photoVC=[[PhotoViewController alloc]initWithNibName:@"PhotoViewController" bundle:nil]; 
     photoVC.photoImage=image; 
     photoVC.photoDelegate=self; 
     [self.navigationController pushViewController:photoVC animated:YES]; 
    } 


} 

回答

0

如果你想攝取的圖像保存到圖片庫,然後用/ in didFinishPickingImage

諸如此類寫UIImageWriteToSavedPhotosAlbum(UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo); 功能,

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage : (UIImage *)image editingInfo:(NSDictionary *)editingInfo 
{  
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); 
    [picker dismissModalViewControllerAnimated:NO]; 
} 
1

我認爲這會幫助你。請試試這個。

這是使用相機拍攝的圖像代碼:

UIImagePickerController *myImagePickerController = [[UIImagePickerController alloc]init]; 
myImagePickerController.delegate = self; 
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera; 
if (YES == [UIImagePickerController isSourceTypeAvailable:sourceType]) 
    { 
     myImagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; 
     [self.view addSubview:myImagePickerController.view]; 
    } 
    else 
    { 
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Warning !!" message:@"Camera is not available" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alertView show]; 
     [alertView release]; 
    } 

此代碼對圖片庫保存圖像:

UIImage *img = [UIImage imageNamed:@"image.png"]; // This is your after taking photo 
    UIImageWriteToSavedPhotosAlbum(img, self, @selector(image:didFinishSavingWithError:contextInfo:), nil); 


    - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo 
    { 
if (error != NULL) 
    { 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!" message:[[error localizedDescription] capitalizedString] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
    } 
    else 
    { 


    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sucesses !!" message:@"Your image has been saved sucessefully." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alert show]; 
      [alert setTag:100]; 
    [alert release]; 
    } 

} 
0
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ if([info objectForKey:@"UIImagePickerControllerOriginalImage"]) 
{ 

    UIImage *tmpimage = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType]; 
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) 
    { 
     if ((picker.sourceType=UIImagePickerControllerSourceTypeCamera)) { 
      UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; 
      UIImageWriteToSavedPhotosAlbum(image,nil,nil,nil); 
     } 
     PhotoViewController *photoVC=[[PhotoViewController alloc]initWithNibName:@"PhotoViewController" bundle:nil]; 
     photoVC.photoImage=tmpimage; 
     photoVC.photoDelegate=self; 
     [self.navigationController pushViewController:photoVC animated:YES]; 
    } 


} 
    } 
相關問題