场景: 在一个VC中,为一个UICollectionViewCell中的图片添加长按图片保存的事件。
长按保存图片
image.png
@interface CustomerImageViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIActionSheetDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;
@property (weak, nonatomic) IBOutlet UILabel *pageLabel;
@property (nonatomic, strong) NSMutableArray<NSString *> *imageUrlStrArr;
@property (nonatomic, strong) UIImage *tempImage;
#pragma - mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.imageUrlStrArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([MyCollectionViewCell class]) forIndexPath:indexPath];
//传数据
[cell setImageUrlStr:self.imageUrlStrArr[indexPath.item]];
//事件
UILongPressGestureRecognizer *ges = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
[cell.imageView addGestureRecognizer:ges];
return cell;
}
-(void)longPressAction:(UILongPressGestureRecognizer*)gesture {
if(gesture.state == UIGestureRecognizerStateBegan)
{
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消"destructiveButtonTitle:nil otherButtonTitles:@"保存图片",nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[actionSheet showInView:self.view];
UIImageView *imgView = (UIImageView*)[gesture view];
_tempImage = imgView.image;
}
}
#pragma - mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet*)actionSheet didDismissWithButtonIndex: (NSInteger)buttonIndex
{
if(buttonIndex ==0) {
if(_tempImage){
UIImageWriteToSavedPhotosAlbum(_tempImage,self,@selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:),nil);
}else{
[Toast showCenterWithText:@"保存失败:没有网络"];
}
}
}
- (void)imageSavedToPhotosAlbum:(UIImage*)image didFinishSavingWithError: (NSError*)error contextInfo:(void*)contextInfo
{
NSString*message =@"";
if(!error) {
message =@"成功保存到相册";
UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"message:message delegate:self cancelButtonTitle:@"确定"otherButtonTitles:nil];
[alert show];
}else{
message = [error description];
UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"message:message delegate:self cancelButtonTitle:@"确定"otherButtonTitles:nil];
[alert show];
}
}
长按保存事件
保存成功提示
@interface CustomerImageViewController ()<UICollectionViewDataSource,UICollectionViewDelegate, UIAlertViewDelegate >
@property (nonatomic , strong) UIAlertView *myAlertView;
@property (nonatomic , strong) UIAlertView *myAlertView2;
-(void)longPressAction:(UILongPressGestureRecognizer*)gesture {
if(gesture.state == UIGestureRecognizerStateBegan)
{
UIImageView *imgView = (UIImageView*)[gesture view];
_tempImage = imgView.image;
self.myAlertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您要保存当前图片到相册中吗?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"保存",nil];
[self.myAlertView show];
}
}
#pragma - mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
// 保存照片(获取到点击的 image)
// NSInteger i = self.scroll.contentOffset.x / self.scroll.bounds.size.width;
if (_tempImage) {
UIImageWriteToSavedPhotosAlbum(_tempImage, self, @selector(image:didFinshSavingWithError:contextInfo:), NULL);
}
}
}
// 保存图片错误提示方法
- (void)image:(UIImage *)image didFinshSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
NSString *mes = nil;
if (error != nil) {
mes = @"保存图片失败";
} else {
mes = @"保存图片成功";
}
self.myAlertView2 = [[UIAlertView alloc] initWithTitle:@"提示" message:mes delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[self.myAlertView2 show];
[NSTimer scheduledTimerWithTimeInterval:0.8f target:self selector:@selector(performDismiss:) userInfo:nil repeats:NO];
}
- (void)performDismiss:(NSTimer *)timer {
[self.myAlertView2 dismissWithClickedButtonIndex:0 animated:YES];
}
长按保存图片
下面的修改点是针对上面的第二节2. UIAlertView的代码
-(void)longPressAction:(UILongPressGestureRecognizer*)gesture {
if(gesture.state == UIGestureRecognizerStateBegan)
{
UIImageView *imgView = (UIImageView*)[gesture view];
_tempImage = imgView.image;
[self handleActionSheet];
}
}
- (void)handleActionSheet{
UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *saveImageAction = [UIAlertAction actionWithTitle:@"保存图片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if (_tempImage) {
UIImageWriteToSavedPhotosAlbum(_tempImage, self, @selector(image:didFinshSavingWithError:contextInfo:), NULL);
}
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[actionSheetController addAction:cancelAction];
[actionSheetController addAction:saveImageAction];
[self presentViewController:actionSheetController animated:YES completion:nil];
}
其中,把preferredStyle的参数UIAlertControllerStyleActionSheet换成UIAlertControllerStyleAlert,就是中部弹框了。
// 保存图片错误提示方法
- (void)image:(UIImage *)image didFinshSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
NSString *mes = nil;
if (error != nil) {
mes = @"保存图片失败";
} else {
mes = @"保存图片成功";
}
self.myAlertView2 = [[UIAlertView alloc] initWithTitle:@"提示" message:mes delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[self.myAlertView2 show];
[NSTimer scheduledTimerWithTimeInterval:0.8f target:self selector:@selector(performDismiss:) userInfo:nil repeats:NO];
}
长按保存图片