保存图片到图片库的方法

 先看文档:


1。保存图片

UIImageWriteToSavedPhotosAlbum

Adds the specified image to the user’s Camera Roll album.

void UIImageWriteToSavedPhotosAlbum (

   UIImage  *image,

   id       completionTarget,

   SEL      completionSelector,

   void     *contextInfo

);

Parameters

image

The image to write to the Camera Roll album.

completionTarget

Optionally, the object whose selector should be called after the image has been written to the Camera Roll album.

completionSelector

The method selector, of the completionTarget object, to call. This optional method should conform to the following signature:

- (void)               image: (UIImage *) image


    didFinishSavingWithError: (NSError *) error


                 contextInfo: (void *) contextInfo;


contextInfo

An optional pointer to any context-specific data that you want passed to the completion selector.

Discussion

When used with an image picker controller, you would typically call this function within yourimagePickerController:didFinishPickingMediaWithInfo: delegate method implementation.

The use of the completionTarget, completionSelector, and contextInfo parameters is optional and necessary only if you want to be notified asynchronously when the function finishes writing the image to the user’s Camera Roll or Saved Photos album. If you do not want to be notified, passnil for these parameters.

When used on an iOS device without a camera, this method adds the image to the Saved Photos album rather than to the Camera Roll album.

Availability

  • Available in iOS 2.0 and later.

See Also

  • UISaveVideoAtPathToSavedPhotosAlbum

Declared In

UIImagePickerController.h



说明:后面三个参数是可选的。如果需要在图片保存成功后进行异步的通知,则后面三个参数是必需的。否则,传递nil。

不太明白 Saved Photos album与Camera Roll album间的区别。。。图片库与相机胶卷。。。感觉在机子里是一回事。

拍照时点击左下角的button会切换到图片库啊???


下面的保存视频的方法与保存图片的方法非常类似,用法基本一样。


下面贴点代码:

(1)最常用的方法

UIImageWriteToSavedPhotosAlbum(UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo);

(2)用AVFoundation库

 There`s much more fast than UIImageWriteToSavedPhotosAlbum way to do it using iOS 4.0+ AVFoundation framework

   ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

    [library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
    if (error) {
    // TODO: error handling
    } else {
    // TODO: success handling
    }
}];
[library release];

主要是以上两种保存图片的方法



具体点:

    //in the .h file put:

NSMutableArray *myPhotoArray;


///then in the .m

- (void) viewDidLoad {

 myPhotoArray = [[NSMutableArray alloc]init];



}

//However Your getting images

- (void) someOtherMethod { 

 UIImage *someImage = [your prefered method of using this];
[myPhotoArray addObject:someImage];

}

-(void) saveMePlease {

//Loop through the array here
for (int i=0:i<[myPhotoArray count]:i++){
         NSString *file = [myPhotoArray objectAtIndex:i];
         NSString *path = [get the path of the image like you would in DOCS FOLDER or whatever];
         NSString *imagePath = [path stringByAppendingString:file];
         UIImage *image = [[[UIImage alloc] initWithContentsOfFile:imagePath]autorelease];

         //Now it will do this for each photo in the array
         UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
        }
}






2。保存视频

UISaveVideoAtPathToSavedPhotosAlbum

Adds the movie at the specified path to the user’s Camera Roll album.

void UISaveVideoAtPathToSavedPhotosAlbum (

   NSString  *videoPath,

   id        completionTarget,

   SEL       completionSelector,

   void      *contextInfo

);

Parameters

videoPath

The filesystem path to the movie file you want to save to the Camera Roll album.

completionTarget

Optionally, the object whose selector should be called after the movie has been written to the Camera Roll album.

completionSelector

The method selector, of the completionTarget object, to call. This optional method should conform to the following signature:

- (void)               video: (NSString *) videoPath


    didFinishSavingWithError: (NSError *) error


                 contextInfo: (void *) contextInfo;


contextInfo

An optional pointer to any context-specific data that you want passed to the completion selector.

Discussion

When used with an image picker controller, you would typically call this function within yourimagePickerController:didFinishPickingMediaWithInfo: delegate method implementation.

Before calling this function, call theUIVideoAtPathIsCompatibleWithSavedPhotosAlbum function to determine if it is possible to save movies to the Camera Roll album. For a code example, refer toCamera Programming Topics for iOS.

The use of the completionTarget, completionSelector, and contextInfo parameters is optional and necessary only if you want to be notified asynchronously when the function finishes writing the movie to the user’s Camera Roll or Saved Photos album. If you do not want to be notified, passnil for these parameters.

When used on an iOS device without a camera, this method adds the movie to the Saved Photos album rather than to the Camera Roll album.

Availability

  • Available in iOS 3.1 and later.

See Also

  • UIImageWriteToSavedPhotosAlbum
  • UIVideoAtPathIsCompatibleWithSavedPhotosAlbum

Declared In

UIImagePickerController.h










  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值