鄭州iOS點-解決拍照照片方向糾偏

照片旋轉是個頭疼的問題,找了很多網(wǎng)上的資料,顯示最多的就是這個:

-(UIImage *)fixFixOrientation;

但是用起來是有問題的,你會發(fā)現(xiàn)前置攝像頭不能糾正倒影成像,拍照界面不能強制豎屏,橫屏拍照無法在豎屏下旋轉至正確的用戶使用方向...;

前人栽樹,拋磚引玉,給大家推薦下我自己的糾偏方案[代碼量其實可以縮進]:

我用的是AVCaptureSession,自定義的一個controller作為容器;

一.跳轉前的配置:

1. 臨時存儲一下設備方向:

NSInteger beforeOrientation = [UIDevice currentDevice].orientation;

2.跳轉前強制豎屏:

[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationPortrait) forKey:@"orientation"];

3.present回調中修正device正確方向:

[self presentViewController:contr animated:YES completion:^{

[[UIDevice currentDevice]setValue:@(beforeOrientation)forKey:@"orientation"];

?}];

說明:這個坑的填法解決了以下幾個問題:

1解決橫豎屏適配的問題,只要豎屏,真爽;

2.解決跳轉前橫屏下檢測不到device正確方向的問題,如果不這樣設置,橫屏跳轉后用戶如果交互沒有喚醒device旋轉監(jiān)聽,image糾偏就需要做更多的處理;

二.拍照回調中:

- (void)captureOutput:(AVCapturePhotoOutput*)output didFinishProcessingPhoto:(nonnull AVCapturePhoto*)photo error:(nullable NSError*)error {

? ? if(!error) {

//? ? ? ? NSLog(@"照片拍攝完成");

? ? ? ? NSData*imageData = [photo fileDataRepresentation];

? ? ? ? UIImage*image = [UIImage imageWithData:imageData];

// 當前輸入設備是前置攝像頭

? ? ? ? if (_currentCameraInput == _frontdeviceInput) {

// 前置攝像頭倒影成像糾偏

? ? ? ? ? ? image = [[UIImage alloc]initWithCGImage:image.CGImage scale:1.0f orientation:UIImageOrientationLeftMirrored];

// 網(wǎng)上流行的fixOrientation升級版

? ? ? ? ? ? image = [image fixOrientationByCameraLocation:YES];

? ? ? ? }else{

? ? ? ? ? ? image = [image fixOrientationByCameraLocation:NO];

? ? ? ? }

// 處理橫屏拍照后,使得image在豎屏下展示正確的用戶使用方向

// device頭朝左

? ? ? ? if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) {

// 橫屏拍照旋轉

? ? ? ? ? ? image = [image landscapefixOrientation:NO];

? ? ? ? }

// device頭朝右

? ? ? ? if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight) {

? ? ? ? ? ? image = [image landscapefixOrientation:YES];

? ? ? ? }

? ? ? ? NSData *da = UIImagePNGRepresentation(image);

// 處理data

? ? ? ? [selfhandleShotDone:da];

? ? }

}

三.然后是寫在延展下的調用方法,這塊是借鑒網(wǎng)友后自己修改的:

- (UIImage*)fixOrientationByCameraLocation:(BOOL)isFront {

? ? NSLog(@"-------%ld",(long)self.imageOrientation);

? ? // No-op if the orientation is already correct

? ? CGAffineTransform transform = CGAffineTransformIdentity;

? ? if (self.imageOrientation == UIImageOrientationUp) return self;

? ? // We need to calculate the proper transformation to make the image upright.

? ? // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.

? ? switch (self.imageOrientation) {

? ? ? ? case UIImageOrientationDown:

? ? ? ? case UIImageOrientationDownMirrored:

? ? ? ? ? ? transform =CGAffineTransformTranslate(transform, self.size.width, self.size.height);

? ? ? ? ? ? transform =CGAffineTransformRotate(transform,M_PI);

? ? ? ? ? ? break;

? ? ? ? case UIImageOrientationLeft:

? ? ? ? case UIImageOrientationLeftMirrored:

? ? ? ? ? ? transform =CGAffineTransformTranslate(transform,self.size.width,0);

? ? ? ? ? ? transform =CGAffineTransformRotate(transform,M_PI_2);

? ? ? ? ? ? break;

? ? ? ? case UIImageOrientationRight:

? ? ? ? case UIImageOrientationRightMirrored:

? ? ? ? ? ? transform =CGAffineTransformTranslate(transform,0,self.size.height);

? ? ? ? ? ? transform =CGAffineTransformRotate(transform, -M_PI_2);

? ? ? ? ? ? break;

? ? ? ? case UIImageOrientationUp:

? ? ? ? case UIImageOrientationUpMirrored:

? ? ? ? ? ? break;

? ? }

? ? switch (self.imageOrientation) {

? ? ? ? case UIImageOrientationUpMirrored:

? ? ? ? case UIImageOrientationDownMirrored:

? ? ? ? ? ? transform =CGAffineTransformTranslate(transform,self.size.width,0);

? ? ? ? ? ? transform =CGAffineTransformScale(transform, -1,1);

? ? ? ? ? ? break;

? ? ? ? case UIImageOrientationLeftMirrored:

? ? ? ? case UIImageOrientationRightMirrored:

? ? ? ? ? ? transform =CGAffineTransformTranslate(transform,self.size.height,0);

? ? ? ? ? ? transform =CGAffineTransformScale(transform, -1,1);

? ? ? ? ? ? break;

? ? ? ? case UIImageOrientationUp:

? ? ? ? case UIImageOrientationDown:

? ? ? ? case UIImageOrientationLeft:

? ? ? ? case UIImageOrientationRight:

? ? ? ? ? ? break;

? ? }

? ? // Now we draw the underlying CGImage into a new context, applying the transform

? ? // calculated above.

? ? CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height,

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? CGImageGetBitsPerComponent(self.CGImage),0,

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? CGImageGetColorSpace(self.CGImage),

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? CGImageGetBitmapInfo(self.CGImage));

? ? CGContextConcatCTM(ctx, transform);

? ? switch (self.imageOrientation) {

? ? ? ? case UIImageOrientationLeft:

? ? ? ? case UIImageOrientationLeftMirrored:

? ? ? ? case UIImageOrientationRight:

? ? ? ? case UIImageOrientationRightMirrored:

? ? ? ? ? ? // Grr...

? ? ? ? ? ? CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage);

? ? ? ? ? ? break;

? ? ? ? default:

? ? ? ? ? ? CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage);

? ? ? ? ? ? break;

? ? }

? ? // And now we just create a new UIImage from the drawing context

? ? CGImageRef cgimg = CGBitmapContextCreateImage(ctx);

? ? UIImage *img = [UIImage imageWithCGImage:cgimg];

? ? CGContextRelease(ctx);

? ? CGImageRelease(cgimg);

? ? returnimg;

}

- (UIImage*)landscapefixOrientation:(BOOL)isleft {

? ? CGAffineTransform transform = CGAffineTransformIdentity;

? ? // No-op if the orientation is already correct

? ? if (self.imageOrientation == UIImageOrientationUp) {

? ? ? ? if(isleft ==YES) {

? ? ? ? ? ? NSLog(@"HOME鍵在左側");

? ? ? ? ? ? transform =CGAffineTransformTranslate(transform,0,self.size.width);

? ? ? ? ? ? transform =CGAffineTransformRotate(transform, -M_PI_2);

? ? ? ? }else{

? ? ? ? ? ? NSLog(@"HOME鍵在右側");

? ? ? ? ? ? transform =CGAffineTransformTranslate(transform,self.size.height,0);

? ? ? ? ? ? transform =CGAffineTransformRotate(transform,M_PI_2);

? ? ? ? }

? ? }else{

? ? }

? ? // Now we draw the underlying CGImage into a new context, applying the transform

? ? // calculated above.

? ? CGContextRef ctx = CGBitmapContextCreate(NULL,

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? self.size.height,

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? self.size.width,

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? CGImageGetBitsPerComponent(self.CGImage),

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0,

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? CGImageGetColorSpace(self.CGImage),? ? //CGImageGetColorSpace(self.CGImage)

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? CGImageGetBitmapInfo(self.CGImage)//CGImageGetBitmapInfo(self.CGImage)

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? );

? CGImageGetBitmapInfo(self.CGImage));

? ? CGContextConcatCTM(ctx, transform);

? ? switch (self.imageOrientation) {

? ? ? ? case UIImageOrientationLeft:

? ? ? ? case UIImageOrientationLeftMirrored:

? ? ? ? case UIImageOrientationRight:

? ? ? ? case UIImageOrientationRightMirrored:

? ? ? ? ? ? // Grr...

? ? ? ? ? ? CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage);

? ? ? ? ? ? break;

? ? ? ? default:

? ? ? ? ? ? CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage);

? ? ? ? ? ? break;

? ? }

? ? // And now we just create a new UIImage from the drawing context

? ? CGImageRef cgimg = CGBitmapContextCreateImage(ctx);

? ? UIImage *img = [UIImage imageWithCGImage:cgimg];

? ? CGContextRelease(ctx);

? ? CGImageRelease(cgimg);

? ? returnimg;

}


另外就是幾個基本的配置:

#pragma mark - 隱藏電池條

- (BOOL)prefersStatusBarHidden {

? ? return YES;

}

#pragma mark - 強制豎屏

-(UIInterfaceOrientationMask)supportedInterfaceOrientations{

? ? return UIInterfaceOrientationMaskPortrait;

}

- (BOOL)shouldAutorotate{

? ? return NO;

}


我自己錄了個屏供大家看下效果:

https://s31.aconvert.com/convert/p3r68-cdx67/eck4f-p9z4l.gif

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末窖贤,一起剝皮案震驚了整個濱河市尺栖,隨后出現(xiàn)的幾起案子怎抛,更是在濱河造成了極大的恐慌箍土,老刑警劉巖览闰,帶你破解...
    沈念sama閱讀 222,104評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件稚照,死亡現(xiàn)場離奇詭異鲫忍,居然都是意外死亡冀墨,警方通過查閱死者的電腦和手機刚夺,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評論 3 399
  • 文/潘曉璐 我一進店門献丑,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人侠姑,你說我怎么就攤上這事创橄。” “怎么了莽红?”我有些...
    開封第一講書人閱讀 168,697評論 0 360
  • 文/不壞的土叔 我叫張陵筐摘,是天一觀的道長。 經(jīng)常有香客問我船老,道長咖熟,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,836評論 1 298
  • 正文 為了忘掉前任柳畔,我火速辦了婚禮馍管,結果婚禮上,老公的妹妹穿的比我還像新娘薪韩。我一直安慰自己确沸,他們只是感情好捌锭,可當我...
    茶點故事閱讀 68,851評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著罗捎,像睡著了一般观谦。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上桨菜,一...
    開封第一講書人閱讀 52,441評論 1 310
  • 那天豁状,我揣著相機與錄音,去河邊找鬼倒得。 笑死泻红,一個胖子當著我的面吹牛,可吹牛的內容都是我干的霞掺。 我是一名探鬼主播谊路,決...
    沈念sama閱讀 40,992評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼菩彬!你這毒婦竟也來了缠劝?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,899評論 0 276
  • 序言:老撾萬榮一對情侶失蹤骗灶,失蹤者是張志新(化名)和其女友劉穎惨恭,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體矿卑,經(jīng)...
    沈念sama閱讀 46,457評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡喉恋,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,529評論 3 341
  • 正文 我和宋清朗相戀三年沃饶,在試婚紗的時候發(fā)現(xiàn)自己被綠了母廷。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,664評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡糊肤,死狀恐怖琴昆,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情馆揉,我是刑警寧澤业舍,帶...
    沈念sama閱讀 36,346評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站升酣,受9級特大地震影響舷暮,放射性物質發(fā)生泄漏。R本人自食惡果不足惜噩茄,卻給世界環(huán)境...
    茶點故事閱讀 42,025評論 3 334
  • 文/蒙蒙 一下面、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧绩聘,春花似錦沥割、人聲如沸耗啦。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,511評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽帜讲。三九已至,卻和暖如春椒拗,著一層夾襖步出監(jiān)牢的瞬間似将,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,611評論 1 272
  • 我被黑心中介騙來泰國打工陡叠, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留玩郊,地道東北人。 一個月前我還...
    沈念sama閱讀 49,081評論 3 377
  • 正文 我出身青樓枉阵,卻偏偏與公主長得像译红,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子兴溜,可洞房花燭夜當晚...
    茶點故事閱讀 45,675評論 2 359