First, the new project
<ignore_js_op>
Second, drag the control, create the map
<ignore_js_op>
Third, add delegate in. h
- @interface Viewcontroller:uiviewcontroller
Copy code Four, implement button events
- -(Ibaction) Chooseimage: (ID) Sender {
- Uiactionsheet *sheet;
- Determine if the camera is supported
- if ([Uiimagepickercontroller Issourcetypeavailable:uiimagepickercontrollersourcetypecamera])
- {
- sheet = [[Uiactionsheet alloc] initwithtitle:@ "select" Delegate:self cancelbuttontitle:nil destructivebuttontitle:@ "Cancel" otherbuttontitles:@ "Photo", @ "select from album", Nil];
- }
- else {
- sheet = [[Uiactionsheet alloc] initwithtitle:@ "select" Delegate:self cancelbuttontitle:nil destructivebuttontitle:@ "Cancel" Otherbuttontitles:@ "Select from album", Nil];
- }
- Sheet.tag = 255;
- [Sheet ShowInView:self.view];
- }
Copy code Five, implement Actionsheet delegate event
- -(void) Actionsheet: (Uiactionsheet *) Actionsheet Clickedbuttonatindex: (Nsinteger) Buttonindex
- {
- if (Actionsheet.tag = = 255) {
- Nsuinteger sourcetype = 0;
- Determine if the camera is supported
- if ([Uiimagepickercontroller Issourcetypeavailable:uiimagepickercontrollersourcetypecamera]) {
- Switch (buttonindex) {
- Case 0:
- Cancel
- Return
- Case 1:
- Camera
- SourceType = Uiimagepickercontrollersourcetypecamera;
- Break
- Case 2:
- Album
- SourceType = uiimagepickercontrollersourcetypephotolibrary;
- Break
- }
- }
- else {
- if (Buttonindex = = 0) {
- Return
- } else {
- SourceType = Uiimagepickercontrollersourcetypesavedphotosalbum;
- }
- }
- Jump to the camera or album page
- Uiimagepickercontroller *imagepickercontroller = [[Uiimagepickercontroller alloc] init];
- Imagepickercontroller.delegate = self;
- imagepickercontroller.allowsediting = YES;
- Imagepickercontroller.sourcetype = sourcetype;
- [Self Presentviewcontroller:imagepickercontroller Animated:yes completion:^{}];
- [Imagepickercontroller release];
- }
- }
Copy Code VI, implement Imagepicker delegate event
- #pragma mark-image Picker Delegte
- -(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: (NSDictionary *) info
- {
- [Picker Dismissviewcontrolleranimated:yes completion:^{}];
- UIImage *image = [info objectforkey:uiimagepickercontrolleroriginalimage];
- /* Here info has six values
- * UIIMAGEPICKERCONTROLLERMEDIATYPE; An nsstring uttypeimage)
- * UIIMAGEPICKERCONTROLLERORIGINALIMAGE; A UIImage original image
- * UIIMAGEPICKERCONTROLLEREDITEDIMAGE; A UIImage image after cropping
- * UIIMAGEPICKERCONTROLLERCROPRECT; An Nsvalue (CGRect)
- * UIIMAGEPICKERCONTROLLERMEDIAURL; An Nsurl
- * Uiimagepickercontrollerreferenceurl//An nsurl that references a asset in the Assetslibrary framework
- * Uiimagepickercontrollermediametadata//An nsdictionary containing metadata from a captured photo
- */
- Save picture to local, see below
- [Self saveimage:image withname:@ "currentimage.png"];
- NSString *fullpath = [[Nshomedirectory () stringbyappendingpathcomponent:@ "Documents"] stringbyappendingpathcomponent:@ "Currentimage.png"];
- UIImage *savedimage = [[UIImage alloc] initwithcontentsoffile:fullpath];
- IsFullScreen = NO;
- [Self.imageview Setimage:savedimage];
- Self.imageView.tag = 100;
- }
- -(void) Imagepickercontrollerdidcancel: (Uiimagepickercontroller *) picker
- {
- [Self Dismissviewcontrolleranimated:yes completion:^{}];
- }
Copy code VII, save picture High Fidelity compression Picture method
- NSData * UIImageJPEGRepresentation (UIImage *image, CGFloat compressionquality
- )
Copy code This method compresses the picture, but the picture quality is basically the same, and the second parameter is the picture quality parameter.
- #pragma mark-Save picture to sandbox
- -(void) SaveImage: (UIImage *) currentimage withname: (NSString *) imageName
- {
- NSData *imagedata = uiimagejpegrepresentation (currentimage, 0.5);
- Get the Sandbox catalog
- NSString *fullpath = [[Nshomedirectory () stringbyappendingpathcomponent:@ "Documents"] Stringbyappendingpathcomponent:imagename];
- Write a picture to a file
- [ImageData Writetofile:fullpath Atomically:no];
- }
Copy code eight, achieve click on the image Preview function, sliding zoom zoom out, drive the painting
- -(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
- {
- IsFullScreen =!isfullscreen;
- Uitouch *touch = [touches anyobject];
- Cgpoint touchPoint = [Touch LocationInView:self.view];
- Cgpoint imagepoint = Self.imageView.frame.origin;
- Touchpoint.x, Touchpoint.y is the coordinates of the contact.
- Contact in ImageView, click ImageView to zoom in, zoom out again when you click.
- 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)
- {
- Set up an image to enlarge animation
- [UIView Beginanimations:nil Context:nil];
- Animation time
- [UIView Setanimationduration:1];
- if (IsFullScreen) {
- Enlarge size
- Self.imageView.frame = CGRectMake (0, 0, 320, 480);
- }
- else {
- Size reduction
- Self.imageView.frame = CGRectMake (50, 65, 90, 115);
- }
- Commit Animation
- [UIView commitanimations];
- }
- }
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
- Asiformdatarequest *requestreport = [[Asiformdatarequest alloc] Initwithurl: server address];
- NSString *path = [[Nshomedirectory () stringbyappendingpathcomponent:@ "Documents"] stringbyappendingpathcomponent:@ " Currentimage.png "];
- [Requestreport setfile:path forkey:@ "PicturePath"];
- [Requestreport Buildpostbody];
- Requestreport.delegate = self;
- [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> |