ios--保存图片到本地相册

来源:互联网 发布:python类 编辑:程序博客网 时间:2024/05/02 13:10
在一个视图中有一个UIImageView,当单击此UIImageView,将UIImageView中的UIImage所代表的图片保存到Photo Album中。

要用到的方法

UIImageWriteToSavedPhotosAlbum([imageView image], nil, nil, nil);
说明
UIImageWriteToSavedPhotosAlbum是UIKit框架中的一个函数。
这里说一下后面三个参数的含义:

void UIImageWriteToSavedPhotosAlbum (   UIImage  *image,   id       completionTarget,   SEL      completionSelector,   void     *contextInfo);
1.id是target对象

2.sel是selector即target对象上的方法名

3.contextInfo是任意指针,会传递到selector定义的方法上。一般是当完成后调用方法时使用,或者在完成时出错的处理。

通过一个Demo具体演示

首先创建一个长按手势

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(LongPress:)];    longPress.minimumPressDuration = 1.0;    [_scrollView addGestureRecognizer:longPress];
-(void)LongPress:(UILongPressGestureRecognizer *)press{    if (press.state==UIGestureRecognizerStateEnded) {        return;    }else if(press.state==UIGestureRecognizerStateBegan)    {        UIActionSheet *actinSheet=[[UIActionSheet alloc] initWithTitle:@"是否保存图片" delegate:self cancelButtonTitle:@"不保存" destructiveButtonTitle:@"保存" otherButtonTitles:nil, nil];        actinSheet.cancelButtonIndex = actinSheet.numberOfButtons - 1;       [actinSheet showInView:[UIApplication sharedApplication].keyWindow];                   }}

#pragma mark -UIActionSheetDelegate保存图片- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{    if (actionSheet.numberOfButtons - 1 == buttonIndex) {        return;    }    NSString *title=[actionSheet buttonTitleAtIndex:buttonIndex];    if ([title isEqualToString:@"保存"])    {        UIImageWriteToSavedPhotosAlbum([_fullImageView image], nil, nil,nil);        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"存储照片成功"                                                        message:@"已存储在Photos中打开即可查看"                                                       delegate:self                                              cancelButtonTitle:@"OK"                                              otherButtonTitles:nil];        [alert show];    }}




0 0
原创粉丝点击