Photo Album select Avatar or Photo upload avatar to nsdata image Binary format form upload

Source: Internet
Author: User

First, click on the Avatar Image or button in the album Select Photos Back to IMG, network upload avatar to be uploaded with data form

(1) Upload avatar Properties

// picture binary format form upload @property (nonatomic, strong) NSData *imagewithdata;

(2) Avatar Click event

-(void) headimageevent{    NSLog (@ " upload avatar ");    [Self selectphotoalbumwithselectphotohandle:^ (UIImage *img) {        = img;         *datawithimage;         0.3 );         = datawithimage;    }];    }

(3) Open an album or take a photo

/** pop-up box select camera or Photo album @param selectphotohandle selected or taken pictures*/- (void) Selectphotoalbumwithselectphotohandle: (void(^) (UIImage *)) selectphotohandle{Self.selectphotohandle=Selectphotohandle; Uialertcontroller*av =[Uialertcontroller alertcontrollerwithtitle:nil message:nil Preferredstyle:uialertcontrollerstyleactionsheet]    ; Uialertaction*action1 = [Uialertaction actionwithtitle:@"Take Pictures"Style:uialertactionstyledefault handler:^ (Uialertaction *_nonnull Action)    {[Self takephoto];    }]; Uialertaction*action2 = [Uialertaction actionwithtitle:@"Choose from a mobile album"Style:uialertactionstyledefault handler:^ (Uialertaction *_nonnull Action)    {[Self localphoto];    }]; Uialertaction*cancelaction = [Uialertaction actionwithtitle:@"Cancel"Style:uialertactionstylecancel Handler:nil];    [AV addaction:action1];    [AV addaction:action2];    [AV addaction:cancelaction]; [Self Presentviewcontroller:av animated:yes completion:nil];}//start taking pictures-(void) takephoto{Uiimagepickercontrollersourcetype sourcetype=Uiimagepickercontrollersourcetypecamera; if([Uiimagepickercontroller Issourcetypeavailable:uiimagepickercontrollersourcetypecamera]) {UIImagePickerC Ontroller*picker =[[Uiimagepickercontroller alloc] init]; Picker.Delegate=Self ; //set the picture to be edited after the photo is takenPicker.allowsediting =YES; Picker.sourcetype=sourcetype;    [Self Presentviewcontroller:picker animated:yes completion:nil]; }Else{NSLog (@"simulate that the camera cannot be opened, use it in a real machine"); }}//Open a local album-(void) localphoto{Uiimagepickercontroller*picker =[[Uiimagepickercontroller alloc] init]; Picker.sourcetype=uiimagepickercontrollersourcetypephotolibrary; Picker.Delegate=Self ; //set the selected picture to be editedPicker.allowsediting =YES; [Self Presentviewcontroller:picker animated:yes completion:nil];}//when you select a picture, enter here-(void) Imagepickercontroller: (uiimagepickercontroller*) Picker didfinishpickingmediawithinfo: (NSDictionary *) info{UIImage* image = [Info objectforkey:@"Uiimagepickercontrollereditedimage"]; if(self.selectphotohandle) {self.selectphotohandle (image);    } [Picker Dismissviewcontrolleranimated:yes completion:nil]; }

(4) Web upload avatar

- (void) postuserheaderimage{nsmutabledictionary*parameter =[[Nsmutabledictionary alloc]init]; [Parameter Setvalue:usertoken forkey:@"userId"]; [Parameter SetValue:self.nickeNameTextField.text forkey:@"Nickname"]; [[[Netrequest Alloc]init]postrequestwithapinamefordata:@"/robot/usercontroller/updaterecord.do"Parameters:parameter imageWithData:self.imageWithData Imagewithnamefile:@"Portrait"Netrequestprogress:nil netrequestsuccess:^ (Nsinteger code, NSString *msg,IDresponse) {ShowMessage (@"Edit Data Success"); } netrequestfaile:^ (Nsinteger code, NSString *msg,IDresponse) {ShowMessage (@"failed to get network data"); }];}

(5) Upload Avatar method

//upload a picture as a stream file (form upload)- (void) Postrequestwithapinamefordata: (NSString *) API Parameters: (nsdictionary *) param imagewithdata: (NSData *) fileData Imagewithnamefile: (NSString *) NameFile netrequestprogress: (netrequestprogress) Progress netrequestsuccess: (netrequestsuccess) sucess Netrequestfaile: (netrequestfaile) faile{//Request AddressNSString *urlstring = [NSString stringWithFormat:@"%@/%@", BASEURL, API]; //Set the status bar network access to the wheel of the wind[[UIApplication sharedapplication] setnetworkactivityindicatorvisible:yes]; //Network Request Sessionafhttpsessionmanager* session =[Afhttpsessionmanager Manager]; Session.requestSerializer.timeoutInterval= the. F; Session.responseSerializer.acceptableContentTypes= [Session.responseSerializer.acceptableContentTypes setbyaddingobject:@"text/html"]; //Session.responseserializer = [Afhttpresponseserializer serializer]; //Initialization of parametersNsmutabledictionary *Dicparam; if(param) {Dicparam=[Nsmutabledictionary Dictionarywithdictionary:param]; } Else{Dicparam=[Nsmutabledictionary dictionary]; }//[Dicparam setobject:@ "i" forkey:@ "P"]; //Platform//[Dicparam setobject:@ "1.0.0" forkey:@ "V"]; //version[Session post:urlstring Parameters:dicparam Constructingbodywithblock:^(ID<AFMultipartFormData>_nonnull FormData) {        if(Filedata.length >0) {Nstimeinterval time=[[NSDate Date] timeIntervalSince1970]; //Note that IMG is a background given stream file name must correspond interface otherwise upload failed[FormData appendpartwithfiledata:filedata name:namefile filename:[nsstring stringWithFormat:@"%ld.jpg", (unsignedLong) Time] MimeType:@"Image/jpeg"]; }} Progress:^ (Nsprogress *_nonnull uploadprogress) {        if(Progress) {progress (uploadprogress); }} Success:^ (Nsurlsessiondatatask * _nonnull task,ID_nullable Responseobject) {        //Cancel Set status bar steamer[[UIApplication sharedapplication] setnetworkactivityindicatorvisible:no]; //API return ResultsNsinteger NCode = [[Responseobject objectforkey:@"Status"] IntegerValue]; NSString*strmsg = [Responseobject objectforkey:@"msg"];//ID res = [responseobject objectforkey:@ "Data"];                        if(sucess) {sucess (ncode,strmsg,responseobject); }} failure:^ (Nsurlsessiondatatask * _nullable task, Nserror *_nonnull Error) {        //Cancel Set status bar steamer[[UIApplication sharedapplication] setnetworkactivityindicatorvisible:no]; NSString* Errresponse = [[NSString alloc] Initwithdata: (NSData *) Error.userinfo[afnetworkingoperationfailingurlresponsedataerrorkey] encoding:nsutf8stringencoding]; NSLog (@"----%@", Errresponse); if(faile) {Faile ([@"99999"Intvalue],@"Network request failed \ n Please check network settings", nil); }    }];}

Photo Album Select avatar or Photo upload avatar to nsdata image Binary format form upload

Related Article

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.