iOS學習:調用相機,選擇圖片上傳,帶預覽功能

一、新建工程  

211351_Qgf1_735123.jpg



二、拖控件,創建映射

211441_CK1O_735123.png



三、在.h中加入delegate

  1. @interface ViewController : UIViewController
複製代碼
四、實現按鈕事件
  1. -(IBAction)chooseImage:(id)sender {
  2.    
  3.     UIActionSheet *sheet;
  4.    
  5.     // 判斷是否支持相機

  6.     if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])

  7.        {
  8.            sheet  = [[UIActionSheet alloc] initWithTitle:@"選擇" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"拍照",@"從相冊選擇", nil];

  9.        }

  10.     else {
  11.         
  12.         sheet = [[UIActionSheet alloc] initWithTitle:@"選擇" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"從相冊選擇", nil];

  13.     }
  14.    
  15.     sheet.tag = 255;
  16.    
  17.     [sheet showInView:self.view];
  18.    
  19. }
複製代碼
五、實現actionSheet delegate事件
  1. -(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
  2. {
  3.     if (actionSheet.tag == 255) {
  4.         
  5.         NSUInteger sourceType = 0;
  6.         
  7.         // 判斷是否支持相機
  8.         if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
  9.             
  10.             switch (buttonIndex) {
  11.                 case 0:
  12.                     // 取消
  13.                     return;
  14.                 case 1:
  15.                     // 相機
  16.                     sourceType = UIImagePickerControllerSourceTypeCamera;
  17.                     break;
  18.                     
  19.                 case 2:
  20.                     // 相冊
  21.                     sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  22.                     break;
  23.             }
  24.         }
  25.         else {
  26.             if (buttonIndex == 0) {
  27.                
  28.                 return;
  29.             } else {
  30.                 sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
  31.             }
  32.         }
  33.         // 跳轉到相機或相冊頁面
  34.         UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
  35.         
  36.         imagePickerController.delegate = self;
  37.         
  38.         imagePickerController.allowsEditing = YES;
  39.         
  40.         imagePickerController.sourceType = sourceType;
  41.         
  42.         [self presentViewController:imagePickerController animated:YES completion:^{}];
  43.         
  44.         [imagePickerController release];
  45.     }
  46. }
複製代碼
六、實現ImagePicker delegate 事件
  1. #pragma mark - image picker delegte
  2. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
  3. {
  4.     [picker dismissViewControllerAnimated:YES completion:^{}];
  5.    
  6.     UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
  7.     /* 此處info 有六個值
  8.      * UIImagePickerControllerMediaType; // an NSString UTTypeImage)
  9.      * UIImagePickerControllerOriginalImage;  // a UIImage 原始圖片
  10.      * UIImagePickerControllerEditedImage;    // a UIImage 裁剪後圖片
  11.      * UIImagePickerControllerCropRect;       // an NSValue (CGRect)
  12.      * UIImagePickerControllerMediaURL;       // an NSURL   
  13.      * UIImagePickerControllerReferenceURL    // an NSURL that references an asset in the AssetsLibrary framework
  14.      * UIImagePickerControllerMediaMetadata    // an NSDictionary containing metadata from a captured photo
  15.      */
  16.     // 保存圖片至本地,方法見下文
  17.     [self saveImage:image withName:@"currentImage.png"];
  18.    
  19.     NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"currentImage.png"];
  20.    
  21.     UIImage *savedImage = [[UIImage alloc] initWithContentsOfFile:fullPath];
  22.    
  23.     isFullScreen = NO;
  24.     [self.imageView setImage:savedImage];
  25.    
  26.     self.imageView.tag = 100;
  27.    
  28. }
  29. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
  30. {
  31.         [self dismissViewControllerAnimated:YES completion:^{}];
  32. }
複製代碼
七、保存圖片
高保真壓縮圖片方法
  1. NSData * UIImageJPEGRepresentation ( UIImage *image, CGFloat compressionQuality
  2. )
複製代碼
此方法可將圖片壓縮,但是圖片質量基本不變,第二個參數即圖片質量參數。

  1. #pragma mark - 保存圖片至沙盒
  2. - (void) saveImage:(UIImage *)currentImage withName:(NSString *)imageName
  3. {
  4.    
  5.     NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5);
  6.     // 獲取沙盒目錄
  7.    
  8.     NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
  9.     // 將圖片寫入文件
  10.    
  11.     [imageData writeToFile:fullPath atomically:NO];
  12. }
複製代碼
八、實現點擊圖片預覽功能,滑動放大縮小,帶動畫
  1. -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  2. {
  3.    
  4.     isFullScreen = !isFullScreen;
  5.     UITouch *touch = [touches anyObject];
  6.    
  7.     CGPoint touchPoint = [touch locationInView:self.view];
  8.    
  9.     CGPoint imagePoint = self.imageView.frame.origin;
  10.     //touchPoint.x ,touchPoint.y 就是觸點的座標
  11.    
  12.     // 觸點在imageView內,點擊imageView時 放大,再次點擊時縮小
  13.     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)
  14.     {
  15.         // 設置圖片放大動畫
  16.         [UIView beginAnimations:nil context:nil];
  17.         // 動畫時間
  18.         [UIView setAnimationDuration:1];
  19.         
  20.         if (isFullScreen) {
  21.             // 放大尺寸
  22.             
  23.             self.imageView.frame = CGRectMake(0, 0, 320, 480);
  24.         }
  25.         else {
  26.             // 縮小尺寸
  27.             self.imageView.frame = CGRectMake(50, 65, 90, 115);
  28.         }
  29.         
  30.         // commit動畫
  31.         [UIView commitAnimations];
  32.         
  33.     }
  34.    
  35. }
複製代碼
九、上傳圖片,使用ASIhttpRequest類庫實現,由於本文重點不是網絡請求,故不對ASIHttpRequest詳細講述,只貼出部分代碼

  1. ASIFormDataRequest *requestReport  = [[ASIFormDataRequest alloc] initWithURL:服務器地址];

  2. NSString *Path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"currentImage.png"];
  3.               
  4. [requestReport setFile:Path forKey:@"picturepath"];

  5. [requestReport buildPostBody];

  6. requestReport.delegate = self;

  7. [requestReport startAsynchronous];
複製代碼
效果圖如下:

213051_6KaZ_735123.png


          ->

213104_1tNV_735123.png



213155_9QM0_735123.png


      

213212_zjXE_735123.png



213227_JpjJ_735123.png


      

213333_KZ7s_735123.jpg



ps:
1.模擬器無法調用相機;
2.模擬器添加圖片方法:將圖片拖至模擬器主屏,會由模擬器safari打開,長按可保存至模擬器相冊,即可進行模擬器調試了。
3.關於調用相機,是系統自帶的,不知道如何修改英文標題爲中文,如cancel換爲取消等,望知道的大蝦們不吝告知,萬分感謝
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章