iOS call camera, album, upload Avatar

Source: Internet
Author: User
<span id="Label3"></p><p><p>first, The new project</p></p><p align="center"><p align="center"></p></p><p><p></p></p><p><p></p></p><p><p><br>second, Drag the control, create the map<br><br></p></p><p align="center"><p align="center"></p></p><p><p></p></p><p><p></p></p><p><p><br>third, Add delegate in. h<br><br></p></p> <ol> <ol> <li>@interface Viewcontroller:uiviewcontroller</li> </ol> </ol>Copy Code<p><p>Iv. Implementing button Events</p></p> <ol> <li><li>-(ibaction) chooseimage: (id) Sender {</li></li> <li><li></li></li> <li><li>Uiactionsheet *sheet;</li></li> <li><li></li></li> <li><li>Determine if the camera is supported</li></li> <li><li></li></li> <li><li>If ([uiimagepickercontroller issourcetypeavailable:uiimagepickercontrollersourcetypecamera])</li></li> <li><li></li></li> <li><li>{</li></li> <li><li>Sheet = [[uiactionsheet alloc] initwithtitle:@ "select" delegate:self cancelbuttontitle:nil destructivebuttontitle:@ "cancel" otherbuttontitles:@ "photo", @ "select from album", nil];</li></li> <li><li></li></li> <li><li>}</li></li> <li><li></li></li> <li><li>else {</li></li> <li><li></li></li> <li><li>Sheet = [[uiactionsheet alloc] initwithtitle:@ "select" delegate:self cancelbuttontitle:nil destructivebuttontitle:@ "cancel" otherbuttontitles:@ "select from album", nil];</li></li> <li><li></li></li> <li><li>}</li></li> <li><li></li></li> <li><li>Sheet.tag = 255;</li></li> <li><li></li></li> <li><li>[sheet showInView:self.view];</li></li> <li><li></li></li> <li><li>}</li></li> </ol>Copy Code<p><p>V. Realization of the Actionsheet delegate event</p></p> <ol> <li><li>-(void) actionsheet: (uiactionsheet *) actionsheet clickedbuttonatindex: (nsinteger) buttonindex</li></li> <li><li>{</li></li> <li><li>if (actionsheet.tag = = 255) {</li></li> <li><li></li></li> <li><li>Nsuinteger sourcetype = 0;</li></li> <li><li></li></li> <li><li>Determine if the camera is supported</li></li> <li><li>If ([uiimagepickercontroller issourcetypeavailable:uiimagepickercontrollersourcetypecamera]) {</li></li> <li><li></li></li> <li><li>Switch (buttonindex) {</li></li> <li><li>Case 0:</li></li> <li><li>Cancel</li></li> <li><li>Return</li></li> <li><li>Case 1:</li></li> <li><li>Camera</li></li> <li><li>SourceType = uiimagepickercontrollersourcetypecamera;</li></li> <li><li>Break</li></li> <li><li></li></li> <li><li>Case 2:</li></li> <li><li>Album</li></li> <li><li>SourceType = uiimagepickercontrollersourcetypephotolibrary;</li></li> <li><li>Break</li></li> <li><li>}</li></li> <li><li>}</li></li> <li><li>else {</li></li> <li><li>if (buttonindex = = 0) {</li></li> <li><li></li></li> <li><li>Return</li></li> <li><li>} else {</li></li> <li><li>SourceType = uiimagepickercontrollersourcetypesavedphotosalbum;</li></li> <li><li>}</li></li> <li><li>}</li></li> <li><li>Jump to the camera or album page</li></li> <li><li>Uiimagepickercontroller *imagepickercontroller = [[uiimagepickercontroller alloc] init];</li></li> <li><li></li></li> <li><li>Imagepickercontroller.delegate = self;</li></li> <li><li></li></li> <li><li>imagepickercontroller.allowsediting = YES;</li></li> <li><li></li></li> <li><li>Imagepickercontroller.sourcetype = sourcetype;</li></li> <li><li></li></li> <li><li>[self Presentviewcontroller:imagepickercontroller Animated:yes completion:^{}];</li></li> <li><li></li></li> <li><li>[imagepickercontroller release];</li></li> <li><li>}</li></li> <li><li>}</li></li> </ol>Copy Code<p><p>Vi. implementation of the Imagepicker delegate event</p></p> <ol> <li><li>#pragma mark-image Picker Delegte</li></li> <li><li>-(void) imagepickercontroller: (uiimagepickercontroller *) picker didfinishpickingmediawithinfo: (NSDictionary *) Info</li></li> <li><li>{</li></li> <li><li>[picker Dismissviewcontrolleranimated:yes completion:^{}];</li></li> <li><li></li></li> <li><li>UIImage *image = [info objectforkey:uiimagepickercontrolleroriginalimage];</li></li> <li><li>/* here info has six values</li></li> <li><li>* uiimagepickercontrollermediatype; An nsstring Uttypeimage)</li></li> <li><li>* uiimagepickercontrolleroriginalimage; A UIImage original image</li></li> <li><li>* uiimagepickercontrollereditedimage; A UIImage image after cropping</li></li> <li><li>* uiimagepickercontrollercroprect; An nsvalue (cgrect)</li></li> <li><li>* uiimagepickercontrollermediaurl; An Nsurl</li></li> <li><li>* Uiimagepickercontrollerreferenceurl//an Nsurl that references a asset in the Assetslibrary framework</li></li> <li><li>* Uiimagepickercontrollermediametadata//an nsdictionary containing metadata from a captured photo</li></li> <li><li>*/</li></li> <li><li>Save picture to local, see below</li></li> <li><li>[self saveimage:image withname:@ "currentimage.png"];</li></li> <li><li></li></li> <li><li>NSString *fullpath = [[nshomedirectory () stringbyappendingpathcomponent:@ "Documents"] stringbyappendingpathcomponent:@ "currentimage.png"];</li></li> <li><li></li></li> <li><li>UIImage *savedimage = [[UIImage alloc] initwithcontentsoffile:fullpath];</li></li> <li><li></li></li> <li><li>IsFullScreen = NO;</li></li> <li><li>[self.imageview setimage:savedimage];</li></li> <li><li></li></li> <li><li>Self.imageView.tag = 100;</li></li> <li><li></li></li> <li><li>}</li></li> <li><li>-(void) imagepickercontrollerdidcancel: (uiimagepickercontroller *) Picker</li></li> <li><li>{</li></li> <li><li>[self Dismissviewcontrolleranimated:yes completion:^{}];</li></li> <li><li>}</li></li> </ol>Copy Code<p><p>seven, Save the picture<br>High Fidelity compression Picture method</p></p> <ol> <ol> <li>NSData * uiimagejpegrepresentation (UIImage *image, cgfloat compressionquality</li> <li>)</li> </ol> </ol>Copy Code<p><p>This method compresses the picture, but the picture quality is basically the same, and the second parameter is the picture quality Parameter.<br><br></p></p> <ol> <ol> <li>#pragma mark-save picture to sandbox</li> <li>-(void) saveimage: (UIImage *) currentimage withname: (nsstring *) imageName</li> <li>{</li> <li></li> <li>NSData *imagedata = uiimagejpegrepresentation (currentimage, 0.5);</li> <li>Get the Sandbox catalog</li> <li></li> <li>NSString *fullpath = [[nshomedirectory () stringbyappendingpathcomponent:@ "Documents"] stringbyappendingpathcomponent:imagename];</li> <li>Write a picture to a file</li> <li></li> <li>[imageData Writetofile:fullpath atomically:no];</li> <li>}</li> </ol> </ol>Copy Code<p><p>eight, the realization of click on the image Preview function, sliding zoom zoom out, drive the painting</p></p> <ol> <li><li>-(void) touchesbegan: (nsset *) touches withevent: (uievent *) Event</li></li> <li><li>{</li></li> <li><li></li></li> <li><li>IsFullScreen =!isfullscreen;</li></li> <li><li>Uitouch *touch = [touches anyobject];</li></li> <li><li></li></li> <li><li>Cgpoint touchPoint = [touch locationInView:self.view];</li></li> <li><li></li></li> <li><li>Cgpoint Imagepoint = self.imageView.frame.origin;</li></li> <li><li>touchpoint.x, Touchpoint.y is the coordinates of the Contact.</li></li> <li><li></li></li> <li><li>Contact in imageview, click ImageView to zoom in, Zoom out again when you click.</li></li> <li><li>If (imagepoint.x <= touchpoint.x && imagepoint.x +self.imageview.frame.size.width >=touchpoint.x & & Imagepoint.y <= touchpoint.y && imagepoint.y+self.imageview.frame.size.height >= touchPoint.y)</li></li> <li><li>{</li></li> <li><li>Set up an image to enlarge animation</li></li> <li><li>[UIView Beginanimations:nil context:nil];</li></li> <li><li>Animation time</li></li> <li><li>[UIView setanimationduration:1];</li></li> <li><li></li></li> <li><li>If (isfullscreen) {</li></li> <li><li>Enlarge size</li></li> <li><li></li></li> <li><li>Self.imageView.frame = CGRectMake (0, 0, 320, 480);</li></li> <li><li>}</li></li> <li><li>else {</li></li> <li><li>Size reduction</li></li> <li><li>Self.imageView.frame = CGRectMake (50, 65, 90, 115);</li></li> <li><li>}</li></li> <li><li></li></li> <li><li>Commit Animation</li></li> <li><li>[UIView commitanimations];</li></li> <li><li></li></li> <li><li>}</li></li> <li><li></li></li> <li><li>}</li></li> </ol>Copy Code<p><p>nine, upload pictures, The use of ASIHTTPRequest class library implementation, because this article is not the focus of the network request, it is not asihttprequest details, only put part of the code<br><br></p></p> <ol> <ol> <li>Asiformdatarequest *requestreport = [[asiformdatarequest alloc] initwithurl: server address];</li> <li></li> <li>NSString *path = [[nshomedirectory () stringbyappendingpathcomponent:@ "Documents"] stringbyappendingpathcomponent:@ " Currentimage.png "];</li> <li></li> <li>[requestreport Setfile:path forkey:@ "picturepath"];</li> <li></li> <li>[requestreport buildpostbody];</li> <li></li> <li>Requestreport.delegate = self;</li> <li></li> <li>[requestreport startasynchronous];</li> </ol> </ol>Copy Code<p><p>iOS call camera, album, upload Avatar</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.