IOS camera uploads images to the server

Source: Internet
Author: User
<span id="Label3"></p><p><p>I do iOS is not a long time, the first encounter a tricky problem is the camera upload pictures to the server, obviously photo album upload a little problem is not, but the album Every time Not.</p></p><p><p>The following code principle: since the album can, why the camera does not work, because there is no uiimagepickercontrollerreferenceurl, because the camera and album to get the info is not the same, So in order to get this Uiimagepickercontrollerreferenceurl key value, I took the photos with the code in the album, and then take out to use it. Although this method is a bit silly, but also took me a long time.</p></p><p><p>Not much to say, paste Code.</p></p><p><p></p></p><pre><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">take <span style="color: #008000;">Pictures</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Takephoto {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(!<span style="color: #000000;"><span style="color: #000000;">[uiimagepickercontroller Issourcetypeavailable:uiimagepickercontrollersourcetypecamera]) {NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">This device does not support taking pictures, please buy new equipment</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">; } Uiimagepickercontroller</span></span>*imagepicker =<span style="color: #000000;"><span style="color: #000000;">[[uiimagepickercontroller alloc]init]; Imagepicker.</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Delegate</span></span>=<span style="color: #000000;">self <span style="color: #000000;">; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Resource Type</span></span>Imagepicker.sourcetype =<span style="color: #000000;"><span style="color: #000000;">uiimagepickercontrollersourcetypecamera; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">whether to allow editing</span></span>Imagepicker.allowsediting =<span style="color: #000000;"><span style="color: #000000;">YES; [self presentviewcontroller:imagepicker Animated:yes completion:nil];}</span></span></pre><p><p></p></p><p><p>Then support Protocols Uiimagepickercontrollerdelegate and Uinavigationcontrollerdelegate</p></p><pre>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) imagepickercontroller: (uiimagepickercontroller *) Picker didfinishpickingmediawithinfo: (NSDictionary<NSString *,<span style="color: #0000ff;"><span style="color: #0000ff;">ID</span></span>> *<span style="color: #000000;"><span style="color: #000000;">) Info {UIImage</span></span>*image =<span style="color: #000000;"><span style="color: #000000;">[[UIImage alloc]init]; Nsurl</span></span>*imageurl =<span style="color: #000000;"><span style="color: #000000;">[[nsurl alloc]init]; __block NSString</span></span>*<span style="color: #000000;"><span style="color: #000000;">imagefilename; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(picker.sourcetype = =<span style="color: #000000;"><span style="color: #000000;">Uiimagepickercontrollersourcetypecamera) {image</span></span>=<span style="color: #000000;"><span style="color: #000000;">[info objectforkey:uiimagepickercontrolleroriginalimage]; Alassetslibrary</span></span>*library =<span style="color: #000000;"><span style="color: #000000;">[[alassetslibrary alloc] init]; [library writeimagetosavedphotosalbum:[image cgimage] orientation: (alassetorientation) [image imageorientation] completionblock:</span></span>^ (nsurl *asseturl, nserror *<span style="color: #000000;"><span style="color: #000000;">Error) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(error) {NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, error.localizeddescription); }</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">asseturl%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, asseturl); Alassetslibraryassetforurlresultblock Resultblock</span></span>= ^ (alasset *<span style="color: #000000;"><span style="color: #000000;">Myasset) {alassetrepresentation</span></span>*representation =<span style="color: #000000;"><span style="color: #000000;">[myasset defaultrepresentation]; Imagefilename</span></span>=<span style="color: #000000;"><span style="color: #000000;">[representation filename]; NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">imagefilename=%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, imagefilename); NSData</span></span>*imgdata = UIImageJPEGRepresentation (image,<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">); Afhttpsessionmanager</span></span>* Manager =<span style="color: #000000;"><span style="color: #000000;">[afhttpsessionmanager manager]; [manager post:getimagepathurls Parameters:nil constructingbodywithblock:</span></span>^(<span style="color: #0000ff;"><span style="color: #0000ff;">ID</span></span><AFMultipartFormData><span style="color: #000000;"><span style="color: #000000;">_nonnull FormData) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(imgdata!=<span style="color: #000000;"><span style="color: #000000;">Nil) {[formData Appendpartwithfiledata:imgdata name:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Upload_file</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>Filename:imagefilename mimeType:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">image/*</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; }} Progress:nil success:</span></span>^ (nsurlsessiondatatask * _nonnull task,<span style="color: #0000ff;"><span style="color: #0000ff;">ID</span></span><span style="color: #000000;"><span style="color: #000000;">_nullable Responseobject) {NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, responseobject); NSString</span></span>*<span style="color: #0000ff;"><span style="color: #0000ff;">string</span></span>= responseobject[<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">AllData</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; Responseobject</span></span>= [[encryptiontools Sharedencryptiontools]decryptstring:<span style="color: #0000ff;"><span style="color: #0000ff;">string</span></span>Keystring:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">D2vuzgfjcc4zmtu4lsmnus</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">iv:nil]; Nsdictionary</span></span>*dic =<span style="color: #000000;"><span style="color: #000000;">[self jsonstringtoobject:responseobject]; NSString</span></span>*imgpath = dic[<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Data</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>][<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Path</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; [self.requestimgarr addobject:imgpath]; [self.tableview reloaddata]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">You can delete the image you just saved Here.</span></span>} failure:^ (nsurlsessiondatatask * _nullable task, Nserror *<span style="color: #000000;"><span style="color: #000000;">_nonnull Error) {NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, error.localizeddescription); }]; }; Alassetslibrary</span></span>* Assetslibrary =<span style="color: #000000;"><span style="color: #000000;">[[alassetslibrary alloc] init]; [assetslibrary Assetforurl:asseturl Resultblock:resultblock FAI lureblock:nil]; } }]; } [picker Dismissviewcontrolleranimated:yes completion:nil]; [self.tableview reloaddata];}</span></span></pre><p><p>It's not very clear, I hope it helps you a little.</p></p><p><p></p></p><p><p>IOS camera uploads images to the server</p></p></span>

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.