1、新建工程ios
2、拖控件,建立映射服務器
3、在.h中加入delegate網絡
@interface ViewController : UIViewController<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
4、實現按鈕事件app
-(IBAction)chooseImage:(id)sender { UIActionSheet *sheet; <p> // 判斷是否支持相機 </p> if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { sheet = [[UIActionSheet alloc] initWithTitle:@"選擇" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"拍照",@"從相冊選擇", nil]; } else { sheet = [[UIActionSheet alloc] initWithTitle:@"選擇" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"從相冊選擇", nil]; } sheet.tag = 255; [sheet showInView:self.view]; }
5、實現actionSheet delegate事件動畫
-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if (actionSheet.tag == 255) { NSUInteger sourceType = 0; // 判斷是否支持相機 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { switch (buttonIndex) { case 0: // 取消 return; case 1: // 相機 sourceType = UIImagePickerControllerSourceTypeCamera; break; case 2: // 相冊 sourceType = UIImagePickerControllerSourceTypePhotoLibrary; break; } } else { if (buttonIndex == 0) { return; } else { sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; } } <p> // 跳轉到相機或相冊頁面 </p> UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; imagePickerController.delegate = self; imagePickerController.allowsEditing = YES; imagePickerController.sourceType = sourceType; [self presentViewController:imagePickerController animated:YES completion:^{}]; [imagePickerController release]; } }
6、實現ImagePicker delegate 事件ui
#pragma mark - image picker delegte - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [picker dismissViewControllerAnimated:YES completion:^{}]; UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; /* 此處info 有六個值 * UIImagePickerControllerMediaType; // an NSString UTTypeImage) * UIImagePickerControllerOriginalImage; // a UIImage 原始圖片 * UIImagePickerControllerEditedImage; // a UIImage 裁剪後圖片 * UIImagePickerControllerCropRect; // an NSValue (CGRect) * UIImagePickerControllerMediaURL; // an NSURL * UIImagePickerControllerReferenceURL // an NSURL that references an asset in the AssetsLibrary framework * UIImagePickerControllerMediaMetadata // an NSDictionary containing metadata from a captured photo */ // 保存圖片至本地,方法見下文 [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:^{}]; }
NSData * UIImageJPEGRepresentation ( UIImage *image, CGFloat compressionQuality )
此方法可將圖片壓縮,可是圖片質量基本不變,第二個參數即圖片質量參數。 atom
#pragma mark - 保存圖片至沙盒 - (void) saveImage:(UIImage *)currentImage withName:(NSString *)imageName { NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5); // 獲取沙盒目錄 NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName]; // 將圖片寫入文件 [imageData writeToFile:fullPath atomically:NO]; }
-(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 就是觸點的座標 // 觸點在imageView內,點擊imageView時 放大,再次點擊時縮小 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) { // 設置圖片放大動畫 [UIView beginAnimations:nil context:nil]; // 動畫時間 [UIView setAnimationDuration:1]; if (isFullScreen) { // 放大尺寸 self.imageView.frame = CGRectMake(0, 0, 320, 480); } else { // 縮小尺寸 self.imageView.frame = CGRectMake(50, 65, 90, 115); } // commit動畫 [UIView commitAnimations]; } }
9、上傳圖片,使用ASIhttpRequest類庫實現,因爲本文重點不是網絡請求,故不對ASIHttpRequest詳細講述,只貼出部分代碼spa
ASIFormDataRequest *requestReport = [[ASIFormDataRequest alloc] initWithURL:服務器地址]; NSString *Path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"currentImage.png"]; [requestReport setFile:Path forKey:@"picturepath"]; [requestReport buildPostBody]; requestReport.delegate = self; [requestReport startAsynchronous];
效果圖以下:.net
1.模擬器沒法調用相機;
2.模擬器添加圖片方法:將圖片拖至模擬器主屏,會由模擬器safari打開,長按可保存至模擬器相冊,便可進行模擬器調試了。
3.關於調用相機,是系統自帶的,不知道如何修改英文標題爲中文,如cancel換爲取消等,望知道的大蝦們不吝告知,萬分感謝
115網盤禮包接收地址: http://115.com/lb/5lbqrhfl
115網盤禮包碼:5lbqrhfl
code4App下載地址:http://code4app.com/ios/51a8023c6803fa2b06000000