[ios開發]鎖屏后的相機的方向檢查,與圖片的自動旋轉


關鍵詞: imageOrientation, 自動旋轉, 獲取方向, 鎖屏, 圖片方向, 自定義拍照
問題描述:


一個同事開發iphone拍照后為圖片添加濾鏡的功能。 發現添加濾鏡時總出現方向自動變化的問題。 


分析原因:

1 方向沒有搞正確。 

2 給濾鏡方法時圖片沒有正過來。 


1 方向沒有搞正確。

解決方法:

因為UIImage本身就自帶的imageOrientation的 值, 一般情況下調用相機拍照會自動為照片添加imageOrientation. 但我們使用的是自定義相機功能,所以在拍照時需要為先設定方向值。 而獲取方向使用到了通知, 在鎖屏的情況下, 方向通知就無法獲取了。見如下代碼。  google無數頁面后, 可以在鎖屏方式仍可獲取方向的方法。 見如下:


// 通過通知獲取方向:(在鎖屏方式下無法獲取方向) 


[notificationCenter addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];


- (void)deviceOrientationDidChange

{

UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];

    

if (deviceOrientation == UIDeviceOrientationPortrait)

orientation = AVCaptureVideoOrientationPortrait;

elseif (deviceOrientation == UIDeviceOrientationPortraitUpsideDown)

orientation = AVCaptureVideoOrientationPortraitUpsideDown;

// AVCapture and UIDevice have opposite meanings for landscape left and right (AVCapture orientation is the same as UIInterfaceOrientation)

elseif (deviceOrientation == UIDeviceOrientationLandscapeLeft)

orientation = AVCaptureVideoOrientationLandscapeRight;

elseif (deviceOrientation == UIDeviceOrientationLandscapeRight)

orientation = AVCaptureVideoOrientationLandscapeLeft;

// Ignore device orientations for which there is no corresponding still image orientation (e.g. UIDeviceOrientationFaceUp)

}



// 通過加速器獲取方向

參考了老外的博客: http://blog.sallarp.com/iphone-accelerometer-device-orientation/


1

[[UIAccelerometersharedAccelerometer] setDelegate:self];


2

@interface AVCamCaptureManager : NSObject<UIAccelerometerDelegate>{

    UIInterfaceOrientation deviceOrientation;

}


@property (nonatomic) UIInterfaceOrientation deviceOrientation;


3


#pragma mark -- 方向


- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration

{

// Get the current device angle

float xx = -[acceleration x];

float yy = [acceleration y];

float angle = atan2(yy, xx);

// Read my blog for more details on the angles. It should be obvious that you

// could fire a custom shouldAutorotateToInterfaceOrientation-event here.

if(angle >= -2.25 && angle <= -0.25)

{

if(deviceOrientation != UIInterfaceOrientationPortrait)

{

deviceOrientation = UIInterfaceOrientationPortrait;

}

}

else if(angle >= -1.75 && angle <= 0.75)

{

if(deviceOrientation != UIInterfaceOrientationLandscapeRight)

{

deviceOrientation = UIInterfaceOrientationLandscapeRight;

}

}

else if(angle >= 0.75 && angle <= 2.25)

{

if(deviceOrientation != UIInterfaceOrientationPortraitUpsideDown)

{

deviceOrientation = UIInterfaceOrientationPortraitUpsideDown

}

}

else if(angle <= -2.25 || angle >= 2.25)

{

if(deviceOrientation != UIInterfaceOrientationLandscapeLeft)

{

deviceOrientation = UIInterfaceOrientationLandscapeLeft

}

}

    orientation = deviceOrientation

    

}


// 自定義拍照方式設置方向:


[stillImageConnection setVideoOrientation:deviceOrientation];


代碼如下: 

- (void) captureStillImage

{

    AVCaptureConnection *stillImageConnection = [AVCamUtilitiesconnectionWithMediaType:AVMediaTypeVideofromConnections:[[selfstillImageOutput] connections]];

 

    if ([stillImageConnection isVideoOrientationSupported])

    {

        NSLog(@"device orientation: %d", deviceOrientation);

        [stillImageConnection setVideoOrientation:deviceOrientation];

    }



2 給濾鏡方法時圖片沒有正過來。 

生成后的圖片會自帶一個imageOrientation的值, 默認情況下UIImageView會自動判斷這個值之后做旋轉顯示, 但我們自己如果要畫到頁面上時就需要對圖片做相就的旋轉: 

google到的: 

http://stackoverflow.com/questions/5427656/ios-uiimagepickercontroller-result-image-orientation-after-upload/5427890#5427890


// 自動旋轉圖片


- (UIImage* )rotateImage:(UIImage *)image {

    int kMaxResolution = 320;

    // Or whatever

    CGImageRef imgRef = image.CGImage;

    CGFloat width = CGImageGetWidth(imgRef);

    CGFloat height = CGImageGetHeight(imgRef);

    CGAffineTransform transform = CGAffineTransformIdentity;

    CGRect bounds = CGRectMake(0, 0, width, height);

    if (width > kMaxResolution || height > kMaxResolution) {

        CGFloat ratio = width  /  height;

        if (ratio > 1 ) {

            bounds.size.width = kMaxResolution;

            bounds.size.height = bounds.size.width / ratio;

        }

        else {

            bounds.size.height = kMaxResolution;

            bounds.size.width = bounds.size.height * ratio;

        }

    }

    CGFloat scaleRatio = bounds.size.width / width;

    CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));

    CGFloat boundHeight;

    UIImageOrientation orient = image.imageOrientation;

    switch (orient) {

        caseUIImageOrientationUp:

            //EXIF = 1

            transform = CGAffineTransformIdentity;

            break;

        caseUIImageOrientationUpMirrored:

            //EXIF = 2

            transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);

            transform = CGAffineTransformScale(transform, -1.0, 1.0 );

            break;

        caseUIImageOrientationDown:

            //EXIF = 3

            transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);

            transform = CGAffineTransformRotate(transform, M_PI);

            break;

        caseUIImageOrientationDownMirrored:

            //EXIF = 4

            transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);

            transform = CGAffineTransformScale(transform, 1.0, -1.0);

            break;

        caseUIImageOrientationLeftMirrored:

            //EXIF = 5

            boundHeight = bounds.size.height;

            bounds.size.height = bounds.size.width;

            bounds.size.width = boundHeight;

            transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width );

            transform = CGAffineTransformScale(transform, -1.0, 1.0);

            transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0 );

            break;

        caseUIImageOrientationLeft:

            //EXIF = 6

            boundHeight = bounds.size.height;

            bounds.size.height = bounds.size.width;

            bounds.size.width = boundHeight;

            transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);

            transform = CGAffineTransformRotate( transform, 3.0 * M_PI / 2.0  );

            break;

        caseUIImageOrientationRightMirrored:

            //EXIF = 7

            boundHeight = bounds.size.height;

            bounds.size.height = bounds.size.width;

            bounds.size.width = boundHeight;

            transform = CGAffineTransformMakeScale(-1.0, 1.0);

            transform = CGAffineTransformRotate( transform, M_PI / 2.0);

            break;

        caseUIImageOrientationRight:

            //EXIF = 8

            boundHeight = bounds.size.height;

            bounds.size.height = bounds.size.width;

            bounds.size.width = boundHeight;

            transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);

            transform = CGAffineTransformRotate(transform, M_PI / 2.0 );

            break;

        default:

            [NSExceptionraise:NSInternalInconsistencyExceptionformat:@"Invalid image orientation"];

    }

    UIGraphicsBeginImageContext(bounds.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) {

        CGContextScaleCTM(context, -scaleRatio, scaleRatio);

        CGContextTranslateCTM(context, -height, 0);

    }

    else {

        CGContextScaleCTM(context, scaleRatio, -scaleRatio);

        CGContextTranslateCTM(context, 0, -height);

    }

    CGContextConcatCTM(context, transform );

    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);

    UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return imageCopy;

}





    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM