Elton's Blog

将图片保存在iPhone的相册中

by on 十一.16, 2009, under iPhone

有时候你的应用需要将应用中的图片保存到用户iPhone或者iTouch的相册中。 可以使用UIKit的这个类方法来完成。

1
2
3
4
5
6
void UIImageWriteToSavedPhotosAlbum (
   UIImage  *image,
   id       completionTarget,
   SEL      completionSelector,
   void     *contextInfo
);

image
要保存到用户设备中的图片

completionTarget
当保存完成后,回调方法所在的对象

completionSelector
当保存完成后,所调用的回调方法。 形式如下:

1
2
3
- (void) image: (UIImage *) image
    didFinishSavingWithError: (NSError *) error
    contextInfo: (void *) contextInfo;

contextInfo
可选的参数,保存了一个指向context数据的指针,它将传递给回调方法。

比如你可以这样来写一个存贮照片的方法:

1
2
3
4
5
// 要保存的图片
  UIImage *img = [UIImage imageNamed:@"ImageName.png"];  
 
  // 保存图片到相册中
  UIImageWriteToSavedPhotosAlbum(img, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

回调方法看起来可能是这样:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error 
             contextInfo:(void *)contextInfo
  {
    // Was there an error?
    if (error != NULL)
    {
      // Show error message...
 
    }
    else  // No errors
    {
      // Show message image successfully saved
    }
  }




:, , ,

2 Comments for this entry

  • wtq

    你好,我使用了这个方法进行截图,但是,接出来的图是上下翻转了180度。
    我的代码如下:
    UIImage *image = img.image;

    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    请问如何解决这种问题。谢谢。

  • wtq

    你好,我在自己的应用程序中通过点击按钮,截取屏幕,然后想重新命名图片再保存到iphone的相册中,可是,我如果将图片重命名了,结果在iphone的相册中仍然显示Img_220.png这种格式的图片。请问有没有什么办法能够实现重命名,并从iphone相册中浏览图片

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit my friends!

A few highly recommended friends...