Currently iOS development, image upload to the server is divided into two, one is directly up to the server, one is the use of third-party storage (reduce server pressure).
First, upload directly to the server
1 /**2 * Code Demo3 */4 //*******uiimagepngrepresentation (uiimage* image) is much larger than the amount of image data returned by uiimagejpegrepresentation (uiimage* image, 1.0), However, from the angle of view, the quality of the picture is not significantly reduced, so when reading the picture data content, you can use UIImageJPEGRepresentation, and time-consuming short. 5 //****** compress the selected picture into a binary file *******photo as a UIImage instance6NSData *data = uiimagejpegrepresentation (Photo,0.5);7 //****** upload pictures from the local to read into the sandbox directory first8 //****** creating a sandbox directory9NSString *docomentpaths = [Nshomedirectory () stringByAppendingPathComponent:@"docomentpaths"];TenNsfilemanager *manager =[Nsfilemanager Defaultmanager]; One [manager createdirectoryatpath:docomentpaths Withintermediatedirectories:yes Attributes:nil Error:nil]; A //* * * * Get current time characters -NSString *timestr = [NSString stringWithFormat:@"%@", [NSDate Date]]; - //* * * * read into the sandbox directory the[Manager createfileatpath:[docomentpaths stringbyappendingstring:[nsstring stringWithFormat:@"%@", Timestr]] Contents:data Attributes:nil]; - //* * * * get Picture path -NSString *filepath = [[NSString Alloc]initwithformat:@"%@%@", Docomentpaths,timestr]; - //****** get the local picture URL +Nsurl *imageurl =[Nsurl Fileurlwithpath:filepath]; -
1 //****url Server Interface address2 //uploading using the AFN network framework3Self.manager =[Afhttprequestoperationmanager manager];4[Self.manager post:url Parameters:dic constructingbodywithblock:^ (ID<AFMultipartFormData>_nonnull FormData) {5 //start uploading formdata submission6[FormData appendpartwithfileurl:imageurl Name:@"Ios_uploadimage"Filename:filepath MimeType:@"Image/jpeg"Error:nil];7 }8 9 //upload multiple images here for Loop commitTen //for (int i = 0; i<self.imagelist.count; i++) One // { A //[FormData appendpartwithfileurl:self.iamgedata[i] name:@ "Heareuy" filename:self.strlist[i] mimetype:@ "ima Ge/jpeg "Error:nil"; - // } - // the -} success:^ (Afhttprequestoperation * _nonnull operation,ID_nonnull Responseobject) { - -} failure:^ (Afhttprequestoperation * _nullable operation, Nserror *_nonnull Error) { + -}];
Second, upload to third-party server storage-Seven Cow cloud storage
The original company's project is also uploaded to the company server, but think of the picture will be very large, with the increase in the number of pictures, the latter is also a problem, so I decided to use the seven Qiniu as a picture storage disk, the process is actually very simple. Backstage partners will be registered on seven of cows, instantiate a disk, on the phone side of the upload, the backstage will give a from seven cattle to get tokens. According to this token mobile phone will upload the image to seven cattle, after the successful upload, seven will return the URL of a picture, and then the URL to the server, plainly, the server just save the phone to upload seven cattle address.
Code demonstration to be continued ...
iOS Development-image upload