簡體   English   中英

將拍攝的圖像保存在照片庫中

[英]Save taken image in photo library

我已經對該程序進行了編碼,以在拾取uiimage時保存圖像,但是我需要編碼以在捕獲UIimagePickerControllerSourceSourcePhotoPhotoLibrary時保存圖像
僅由相機拍攝。

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


}

我認為這會對您有所幫助。 請試試這個。

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

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

}

如果要將選擇的圖像保存到照片庫中,請使用/編寫UIImageWriteToSavedPhotosAlbum(UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo); didFinishPickingImage函數

像這樣

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage : (UIImage *)image editingInfo:(NSDictionary *)editingInfo
{    
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    [picker dismissModalViewControllerAnimated:NO];
}
- (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];
    }


}
   }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM