iOS_長按儲存圖片,ios儲存圖片

來源:互聯網
上載者:User

iOS_長按儲存圖片,ios儲存圖片

最終:
範例程式碼片段


////  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


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.