明天測試項目的改動頭像功用,上午不斷在用模仿器測試,沒有任何問題,然後剛剛運用了真機來測。發現一個問題:昨天拍的貓的照片,上傳上去之後,方向不對!因而就有了本文
緣由由於模仿器是沒有拍照功用的,然後運用ALAsset保管到本地的圖片,方向默許是正確的。
但是真機,由於拍照的時分,會無方向的概念,所以,模仿器上傳不會呈現上述問題。
直接上代碼
#pragma mark - UIImagePickerController delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
if (info[UIImagePickerControllerEditedImage]) {
UIImage *img = (UIImage *)info[UIImagePickerControllerEditedImage];
UIImageOrientation imageOrientation = img.imageOrientation;
[self saveImage:img withDirection:imageOrientation];
}else {
UIImage *img = (UIImage *)info[UIImagePickerControllerOriginalImage];
UIImageOrientation imageOrientation = img.imageOrientation;
[self saveImage:img withDirection:imageOrientation];
}
[picker dismissViewControllerAnimated:true completion:nil];
}
- (void)saveImage:(UIImage *)image withDirection:(UIImageOrientation)direction {
if(direction != UIImageOrientationUp)
{
// 原始圖片可以依據照相時的角度來顯示,但UIImage無法斷定,於是呈現獲取的圖片會向左轉90度的景象。
// 以下為調整圖片角度的局部
UIGraphicsBeginImageContext(image.size);
[image draWinRect:CGRectMake(0, 0, image.size.width, image.size.height)];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// 調整圖片角度終了
}
//獲取圖片的長寬,然後截取其中居中局部的正方形
CGImageRef imgRef = image.CGImage;
CGFloat imgWidth = CGImageGetWidth(imgRef);
CGFloat imgHeight = CGImageGetHeight(imgRef);
CGImageRef clipImgRef;
if (imgWidth > imgHeight) {
clipImgRef = CGImageCreateWithImageInRect(imgRef, CGRectMake((imgWidth - imgHeight)/2, 0, imgHeight, imgHeight));
}else if (imgHeight > imgWidth) {
clipImgRef = CGImageCreateWithImageInRect(imgRef, CGRectMake(0,(imgHeight - imgWidth)/2, imgWidth, imgWidth));
}
UIImage *scaleImg = [UIImage imageWithCGImage:clipImgRef];
NSData *data = UIImageJPEGRepresentation([self scaleFromImage:scaleImg size:CGSizeMake(150, 150)], 1);
self.showImgView.image = [UIImage imageWithData:data];
}
代碼詳見github
【iOS上傳圖片方向不對處置】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!