-(void) viewdidload {[Super viewdidload]; Self.view.backgroundColor = [Uicolor Whitecolor]; /* There are two ways to save a picture: 1>. button mode; 2> Long press the picture; *//Display picture _imagev = [[Uiimageview alloc]initwithframe:cgrectmake (100, 100, 200, 200)]; [Note?]: "9.jpg" Here is the name of the picture name, the user changed to the corresponding picture name _imagev.image = [UIImage imagenamed:@ "9.jpg"];//Use gestures must turn on interactivity_imagev.userinteractionenabled = YES; [Self.view addsubview:_imagev]; //Way One: Add a long press gesture to the picture Uilongpressgesturerecognizer * longpress = [Uilo Ngpressgesturerecognizer alloc]initwithtarget:self Action: @selector (Longpressclick:)]; //Set long time between , default 0.5 seconds longpress.minimumpressduration = 1.0; [Self.imagev addgesturerecognizer:longpress]; //Mode two: Create button UIButton * btn = [UIButt On buttonwithtype:uibuttontypecustom]; Btn.backgroundcolor = [Uicolor Yellowcolor]; [Btn settitle:@ "Save album" Forstate:uicontrolstatenormal]; [Btn settitlecolor:[uicolor Orangecolor] forstate:uicontrolstatenormal]; Btn.frame =cgrectmake (30, 70, 100, 30); [Self.view ADDSUBVIEW:BTN]; [Btn addtarget:self Action: @selector (Btnclick:) forcontrolevents:uicontroleventtouchupinside];} //Long press gesture to achieve picture preservation-(void) Longpressclick: (Uigesturerecognizer *) longpress{ //must add a judgment statement to prevent multiple saves if (longpress.state = = Uigesturerecognizerstatebegan) { Uiimagewritetosavedphotosalbum (self.imageV.image, Self, @selector (imagesavedtophotosalbum: Didfinishsavingwitherror:contextinfo:), nil); }} //button click event Implementation-(void) Btnclick: ( uibutton *) btn{uiimagewritetosavedphotosalbum (Self.imageV.image, Self, @selector (imagesavedtophotosalbum: Didfinishsavingwitherror:contextinfo:), nil);} //How to save a picture-(void) Imagesavedtophotosalbum: (UIImage *) image didfinishsavingwitherror: (Nserror *) error ContextInfo: (void *) contextinfo{if (!error) { NSLog (@ "Success picture saved to album"); }else{ NSLog (@ "%@", Error.localizeddescription); }}
How to save a picture in a local album in iOS development