将拍摄的图像保存在照片库中

时间:2014-05-10 12:56:01

标签: ios objective-c

我已经编写了这个程序来保存图像,同时选择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];
    }


}

3 个答案:

答案 0 :(得分: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];
  }

}

答案 1 :(得分:0)

如果您想将已挑选的图片保存到照片库,请在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];
}

答案 2 :(得分: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];
    }


}
   }