首页 编程 软件学院 查看内容

iOS_长按保存图片

2015-3-27 17:32 913 0

摘要: 最终效果图: 示例代码片段 [objc] view plaincopyprint?//  //  ViewCon...
关键词: nbsp image self selector progress UIButton 图片 contextInfo nil imageView

最终效果图: 示例代码片段 [objc] view plaincopyprint?//  //  ViewController.m  //  长按,弹出 菜单控制器 下载图片  //  //  Created by beyond on 15-3-26.  //  Copyright (c) 2015年 beyond.com All rights reserved.  //    #import "ViewController.h"    @interface ViewController ()  @property (weak, nonatomic) IBOutlet UIImageView *imageView;    @end    @implementation ViewController    - (void)viewDidLoad {      [super viewDidLoad];                  // 新增添加长按手势      UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longTap:)];      [self.view addGestureRecognizer:longTap];        }  // 新增添加长按手势  - (void)longTap:(UILongPressGestureRecognizer *)reco  {      if ([reco state]==UIGestureRecognizerStateBegan) {          [[reco view]becomeFirstResponder];          CGPoint loc = [reco locationInView:nil];          UIMenuController *mCtrl = [UIMenuController sharedMenuController];          UIMenuItem *item = [[UIMenuItem alloc]initWithTitle:@"保存" action:@selector(saveClick:)];          [mCtrl setMenuItems:[NSArray arrayWithObject:item]];          // 这儿有问题!!!!          //        CGPoint loc = reco.                    [mCtrl setTargetRect:CGRectMake(loc.x, loc.y-20, 0, 0) inView:[reco view] ];          [mCtrl setMenuVisible:YES animated:YES];      }  }  // 必须实现  - (BOOL)canBecomeFirstResponder{      return YES;  }  -(BOOL) canPerformAction:(SEL)action withSender:(id)sender{      if (action == @selector(saveClick:) ) {          return YES;      }      return NO; //隐藏系统默认的菜单项  }  // 新增添加长按手势  - (void)saveClick:(id)sender  {      if (self.imageView.image) {          UIImageWriteToSavedPhotosAlbum(self.imageView.image, nil, nil, nil);          UIImageWriteToSavedPhotosAlbum(self.imageView.image, self,                                         @selector(image:didFinishSavingWithError:contextInfo:), nil);      }        }  // 写到文件的完成时执行  - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(voidvoid *)contextInfo {      //[SVProgressHUD showSuccessWithStatus:@"图片已储存"];  //    UIButton *btn = (UIButton *)[_footView viewWithTag:VHelpDetailFootViewBtnTypeFont];  //    btn.enabled = YES;  }  #pragma mark - 补充 下载图片  - (void)downBtnClick:(UIButton *)btn  {      UIButton *btn = (UIButton *)[_footView viewWithTag:VHelpDetailFootViewBtnTypeFont];      btn.enabled = NO;      if ([_currentPhoto underlyingImage]) {          // 直接写到文件          [self writeImgToFile];      } else {          // 改成新的 远程下载图片          [self downloadImgAsync];      }  }  //下载图片  #import "SDWebImageDecoder.h"  #import "SDWebImageManager.h"  #import "SDWebImageOperation.h"  - (void)downloadImgAsync  {    // Load async from web (using SDWebImage)      [SVProgressHUD showSuccessWithStatus:@"图片保存中"];      @try {          [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:_currentImgUrl]  options:0  progress:^(NSInteger receivedSize, NSInteger expectedSize) {              if (expectedSize > 0) {                  float progress = receivedSize / (float)expectedSize;                  NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:                                        [NSNumber numberWithFloat:progress], @"progress",                                        self, @"photo", nil nil];                  [[NSNotificationCenter defaultCenter] postNotificationName:MWPHOTO_PROGRESS_NOTIFICATION object:dict];              }          }                                                        completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {                                                            if (error) {                                                                MWLog(@"SDWebImage failed to download image: %@", error);                                                            }  // 写到文件                                                            UIImageWriteToSavedPhotosAlbum(image, self,                                                                                           @selector(image:didFinishSavingWithError:contextInfo:), nil);                                                        }];      } @catch (NSException *e) {          MWLog(@"Photo from web: %@", e);      }  }  @end 
声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系 [邮箱地址] 删除

路过

雷人

握手

鲜花

鸡蛋

最新评论

返回顶部