二、拖控件,創建映射
三、在.h中加入delegate
- @interface ViewController : UIViewController
- -(IBAction)chooseImage:(id)sender {
-
- UIActionSheet *sheet;
-
- // 判斷是否支持相機
- 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];
-
- }
- -(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;
- }
- }
- // 跳轉到相機或相冊頁面
- UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
-
- imagePickerController.delegate = self;
-
- imagePickerController.allowsEditing = YES;
-
- imagePickerController.sourceType = sourceType;
-
- [self presentViewController:imagePickerController animated:YES completion:^{}];
-
- [imagePickerController release];
- }
- }
- #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
- )
- #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];
-
- }
-
- }
- 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];
->
ps:
1.模擬器無法調用相機;
2.模擬器添加圖片方法:將圖片拖至模擬器主屏,會由模擬器safari打開,長按可保存至模擬器相冊,即可進行模擬器調試了。
3.關於調用相機,是系統自帶的,不知道如何修改英文標題爲中文,如cancel換爲取消等,望知道的大蝦們不吝告知,萬分感謝