iOS_长按保存图片

最终效果图:
iOS_长按保存图片_第1张图片
示例代码片段



//
//  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:(void *)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];
                [[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


你可能感兴趣的:(ios,异步下载,MenuController)