資源描述:
《ios從相機或相冊獲取圖片并裁剪》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在工程資料-天天文庫。
1、iOS從相機或相冊獲取圖片并裁剪需要從相冊或者相機中讀取圖片。代碼很簡單,抽取關(guān)鍵部分,如下:?//loaduserimage-(void)UesrImageClicked{UIActionSheet*sheet;//判斷是否支持相機if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){sheet=[[UIActionSheetalloc]initWithTitle:@"選擇圖像"delegate:selfcancelButtonTitle:nild
2、estructiveButtonTitle:@"取消"otherButtonTitles:@"拍照",@"從相冊選擇",nil];}else{sheet=[[UIActionSheetalloc]initWithTitle:@"選擇圖像"delegate:selfcancelButtonTitle:nildestructiveButtonTitle:@"取消"otherButtonTitles:@"從相冊選擇",nil];}sheet.tag=255;[sheetshowInView:self.view];}#pragmamark-actionsheetdelegte-(vo
3、id)actionSheet:(UIActionSheet*)actionSheetclickedButtonAtIndex:(NSInteger)buttonIndex{if(actionSheet.tag==255){NSUIntegersourceType=UIImagePickerControllerSourceTypePhotoLibrary;//判斷是否支持相機if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){switch(butto
4、nIndex){case0:return;case1://相機sourceType=UIImagePickerControllerSourceTypeCamera;break;case2://相冊sourceType=UIImagePickerControllerSourceTypePhotoLibrary;break;}}else{if(buttonIndex==0){return;}else{sourceType=UIImagePickerControllerSourceTypeSavedPhotosAlbum;}}//跳轉(zhuǎn)到相機或相冊頁面UIImagePickerCon
5、troller*imagePickerController=[[UIImagePickerControlleralloc]init];imagePickerController.delegate=self;imagePickerController.allowsEditing=YES;imagePickerController.sourceType=sourceType;[selfpresentViewController:imagePickerControlleranimated:YEScompletion:^{}];}}#pragmamark-imagepickerdel
6、egte-(void)imagePickerController:(UIImagePickerController*)pickerdidFinishPickingMediaWithInfo:(NSDictionary*)info{[pickerdismissViewControllerAnimated:YEScompletion:^{}];UIImage*image=[infoUIImagePickerControllerOriginalImage];userImageView.image=image;NSData*imageData=UIImageJPEGRepresent
7、ation(image,COMPRESSED_RATE);UIImage*compressedImage=[UIImageimageWithData:imageData];[HttpRequestManageruploadImage:compressedImagehttpClient:self.httpClientdelegate:self];}創(chuàng)建一張表單,選擇相機或者相冊,然后用回調(diào)函數(shù)處理返回的圖片。但是返回的圖像并不是正方形,顯示在用戶頭像的View里面產(chǎn)生明顯的拉伸。研究了很久各種裁剪算法,甚