IOS _ long-pressed Save image, ios Save image

Source: Internet
Author: User

IOS _ long-pressed Save image, ios Save image

Finally:
Sample code snippet


//// ViewController. m // long press, the menu controller is displayed to download the image /// 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]; // added the long-pressed gesture UILongPressGestureRecognizer * longTap = [UILongPressGestureRecogniz Er alloc] initWithTarget: self action: @ selector (longTap :)]; [self. view addGestureRecognizer: longTap];} // added long-pressed gesture-(void) longTap :( UILongPressGestureRecognizer *) reco {if ([reco state] = UIGestureRecognizerStateBegan) {[[reco view] locate]; CGPoint loc = [reco locationInView: nil]; UIMenuController * mCtrl = [UIMenuController sharedMenuController]; UIMenuItem * item = [UIMenuItem alloc] in ItWithTitle: @ "save" action: @ selector (saveClick :)]; [mCtrl setMenuItems: [NSArray arrayWithObject: item]; // There is a problem !!!! // CGPoint loc = reco. [mCtrl setTargetRect: CGRectMake (loc. x, loc. y-20, 0, 0) inView: [reco view]; [mCtrl setMenuVisible: YES animated: YES] ;}// must implement-(BOOL) canBecomeFirstResponder {return YES ;} -(BOOL) canPerformAction :( SEL) action withSender :( id) sender {if (action = @ selector (saveClick :)) {return YES;} return NO; // hide the default menu items} // Add a long-pressed gesture-(void) saveClick :( id) sender {if (self. imageView. image) {UIImageWriteToSavedPhotosAlbum (self. imageView. image, nil); UIImageWriteToSavedPhotosAlbum (self. imageView. image, self, @ selector (image: didFinishSavingWithError: contextInfo :), nil) ;}// execute when the file is written to complete-(void) image :( UIImage *) image metadata :( NSError *) error contextInfo :( void *) contextInfo {// [SVProgressHUD showSuccessWithStatus: @ "image saved"]; // UIButton * btn = (UIButton *) [_ footView viewWithTag: VHelpDetailFootViewBtnTypeFont]; // btn. enabled = YES ;}# pragma mark-supplemental download image-(void) downBtnClick :( UIButton *) btn {UIButton * btn = (UIButton *) [_ footView viewWithTag: done]; btn. enabled = NO; if ([_ currentPhoto underlyingImage]) {// directly write to the file [self writeImgToFile];} else {// change to the new remote download image [self downloadImgAsync] ;}}// download image # import "SDWebImageDecoder. h "# import" SDWebImageManager. h "# import" SDWebImageOperation. h "-(void) downloadImgAsync {// Load async from web (using SDWebImage) [SVProgressHUD progress: @" saving images "]; @ 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 Syntax: [NSNumber numberWithFloat: progress], @ "progress", self, @ "photo", nil]; [[nsicationicationcenter defacenter] postNotificationName: Your object: dict] ;}} completed: ^ (UIImage * image, NSError * error, SDImageCacheType cacheType, BOOL finished, NSURL * imageURL) {if (error) {MWLog (@ "SDWebImage failed to download image: % @", error);} // write to the file UIImageWriteToSavedPhotosAlbum (image, self, @ selector (image: didFinishSavingWithError: contextInfo :), nil) ;}] ;}@ catch (NSException * e) {MWLog (@ "Photo from web: % @", e) ;}@ end


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.