iOS Learning: Call camera, select Image upload, with preview function

Source: Internet
Author: User

First, the new project

<ignore_js_op>



Second, drag the control, create the map

<ignore_js_op>



Third, add delegate in. h

    1. @interface Viewcontroller:uiviewcontroller
Copy code Four, implement button events
  1. -(Ibaction) Chooseimage: (ID) Sender {
  2. Uiactionsheet *sheet;
  3. Determine if the camera is supported
  4. if ([Uiimagepickercontroller Issourcetypeavailable:uiimagepickercontrollersourcetypecamera])
  5. {
  6. sheet = [[Uiactionsheet alloc] initwithtitle:@ "select" Delegate:self cancelbuttontitle:nil destructivebuttontitle:@ "Cancel" otherbuttontitles:@ "Photo", @ "select from album", Nil];
  7. }
  8. else {
  9. sheet = [[Uiactionsheet alloc] initwithtitle:@ "select" Delegate:self cancelbuttontitle:nil destructivebuttontitle:@ "Cancel" Otherbuttontitles:@ "Select from album", Nil];
  10. }
  11. Sheet.tag = 255;
  12. [Sheet ShowInView:self.view];
  13. }
Copy code Five, implement Actionsheet delegate event
  1. -(void) Actionsheet: (Uiactionsheet *) Actionsheet Clickedbuttonatindex: (Nsinteger) Buttonindex
  2. {
  3. if (Actionsheet.tag = = 255) {
  4. Nsuinteger sourcetype = 0;
  5. Determine if the camera is supported
  6. if ([Uiimagepickercontroller Issourcetypeavailable:uiimagepickercontrollersourcetypecamera]) {
  7. Switch (buttonindex) {
  8. Case 0:
  9. Cancel
  10. Return
  11. Case 1:
  12. Camera
  13. SourceType = Uiimagepickercontrollersourcetypecamera;
  14. Break
  15. Case 2:
  16. Album
  17. SourceType = uiimagepickercontrollersourcetypephotolibrary;
  18. Break
  19. }
  20. }
  21. else {
  22. if (Buttonindex = = 0) {
  23. Return
  24. } else {
  25. SourceType = Uiimagepickercontrollersourcetypesavedphotosalbum;
  26. }
  27. }
  28. Jump to the camera or album page
  29. Uiimagepickercontroller *imagepickercontroller = [[Uiimagepickercontroller alloc] init];
  30. Imagepickercontroller.delegate = self;
  31. imagepickercontroller.allowsediting = YES;
  32. Imagepickercontroller.sourcetype = sourcetype;
  33. [Self Presentviewcontroller:imagepickercontroller Animated:yes completion:^{}];
  34. [Imagepickercontroller release];
  35. }
  36. }
Copy Code VI, implement Imagepicker delegate event
  1. #pragma mark-image Picker Delegte
  2. -(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: (NSDictionary *) info
  3. {
  4. [Picker Dismissviewcontrolleranimated:yes completion:^{}];
  5. UIImage *image = [info objectforkey:uiimagepickercontrolleroriginalimage];
  6. /* Here info has six values
  7. * UIIMAGEPICKERCONTROLLERMEDIATYPE; An nsstring uttypeimage)
  8. * UIIMAGEPICKERCONTROLLERORIGINALIMAGE; A UIImage original image
  9. * UIIMAGEPICKERCONTROLLEREDITEDIMAGE; A UIImage image after cropping
  10. * UIIMAGEPICKERCONTROLLERCROPRECT; An Nsvalue (CGRect)
  11. * UIIMAGEPICKERCONTROLLERMEDIAURL; An Nsurl
  12. * Uiimagepickercontrollerreferenceurl//An nsurl that references a asset in the Assetslibrary framework
  13. * Uiimagepickercontrollermediametadata//An nsdictionary containing metadata from a captured photo
  14. */
  15. Save picture to local, see below
  16. [Self saveimage:image withname:@ "currentimage.png"];
  17. NSString *fullpath = [[Nshomedirectory () stringbyappendingpathcomponent:@ "Documents"] stringbyappendingpathcomponent:@ "Currentimage.png"];
  18. UIImage *savedimage = [[UIImage alloc] initwithcontentsoffile:fullpath];
  19. IsFullScreen = NO;
  20. [Self.imageview Setimage:savedimage];
  21. Self.imageView.tag = 100;
  22. }
  23. -(void) Imagepickercontrollerdidcancel: (Uiimagepickercontroller *) picker
  24. {
  25. [Self Dismissviewcontrolleranimated:yes completion:^{}];
  26. }
Copy code VII, save picture
High Fidelity compression Picture method
    1. NSData * UIImageJPEGRepresentation (UIImage *image, CGFloat compressionquality
    2. )
Copy code This method compresses the picture, but the picture quality is basically the same, and the second parameter is the picture quality parameter.

    1. #pragma mark-Save picture to sandbox
    2. -(void) SaveImage: (UIImage *) currentimage withname: (NSString *) imageName
    3. {
    4. NSData *imagedata = uiimagejpegrepresentation (currentimage, 0.5);
    5. Get the Sandbox catalog
    6. NSString *fullpath = [[Nshomedirectory () stringbyappendingpathcomponent:@ "Documents"] Stringbyappendingpathcomponent:imagename];
    7. Write a picture to a file
    8. [ImageData Writetofile:fullpath Atomically:no];
    9. }
Copy code eight, achieve click on the image Preview function, sliding zoom zoom out, drive the painting
  1. -(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
  2. {
  3. IsFullScreen =!isfullscreen;
  4. Uitouch *touch = [touches anyobject];
  5. Cgpoint touchPoint = [Touch LocationInView:self.view];
  6. Cgpoint imagepoint = Self.imageView.frame.origin;
  7. Touchpoint.x, Touchpoint.y is the coordinates of the contact.
  8. Contact in ImageView, click ImageView to zoom in, zoom out again when you click.
  9. 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)
  10. {
  11. Set up an image to enlarge animation
  12. [UIView Beginanimations:nil Context:nil];
  13. Animation time
  14. [UIView Setanimationduration:1];
  15. if (IsFullScreen) {
  16. Enlarge size
  17. Self.imageView.frame = CGRectMake (0, 0, 320, 480);
  18. }
  19. else {
  20. Size reduction
  21. Self.imageView.frame = CGRectMake (50, 65, 90, 115);
  22. }
  23. Commit Animation
  24. [UIView commitanimations];
  25. }
  26. }
Copy code nine, upload pictures, using the 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

    1. Asiformdatarequest *requestreport = [[Asiformdatarequest alloc] Initwithurl: server address];
    2. NSString *path = [[Nshomedirectory () stringbyappendingpathcomponent:@ "Documents"] stringbyappendingpathcomponent:@ " Currentimage.png "];
    3. [Requestreport setfile:path forkey:@ "PicturePath"];
    4. [Requestreport Buildpostbody];
    5. Requestreport.delegate = self;
    6. [Requestreport startasynchronous];
Copy the code as follows:

<ignore_js_op>


-

<ignore_js_op>



<ignore_js_op>


<ignore_js_op>



<ignore_js_op>


<ignore_js_op>

iOS Learning: Call camera, select Image upload, with preview function

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.