---
title: "存图片和视频到iPhone相册"
author: "wonderzl"
date: "2011-04-19T09:33:10Z"
tags: []
categories: ["移动开发"]
slug: "iphone"
draft: false
lastmod: "2011-04-19T09:33:10Z"
id: 1007642
url: "https://wonderzl.iteye.com/blog/1007642"
---

```Obj-c

    UIImage *img = [uiimage imagenamed:@"daziran.jpg"];  
    
    // 保存图片到相册中
    UIImageWriteToSavedPhotosAlbum(img, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);


```


当保存完成后，调用的回调方法:

```Obj-c

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    // Was there an error?
    if (error != NULL)
    {
        // Show error message…
         NSLog(@"Save image failed");
        
    }
    else  // No errors
    {
        // Show message image successfully saved
        NSLog(@"Save image successful");
    }
}

```


存视频到相册：

```Obj-c

    NSString *path=[[nsbundle mainbundle] pathForResource:@"movie" ofType:@"mp4"];

    if(UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(path))
    {
        UISaveVideoAtPathToSavedPhotosAlbum(path,nil,nil,nil);
    }

    else {
        NSLog(@"no available");
        UIAlertView *alert=[[uialertview alloc] initWithTitle:nil message:@"no available" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
        [alert show];
        [alert release];
    }

```