2011-11-30 2 views
1

사진 앱을 개발 중입니다.카메라 이미지가 iphone에서 방향을 자동으로 변경하고 있습니다.

여기 사진 라이브러리와 이미지를 사용하여 이미지를 볼 수 있습니다.

사진 라이브러리의 이미지가 올바른 방향으로 표시되지만 카메라 이미지가 다른 방향으로 표시됩니다.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder 
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"MyFolder"]; 
    NSString *imagePath = [dataPath stringByAppendingPathComponent:@"girl.jpg"]; 

    if ([[NSFileManager defaultManager] fileExistsAtPath:imagePath]) { 
     UIImage *image = [UIImage imageWithCGImage:[UIImage imageWithContentsOfFile:imagePath].CGImage scale:bieberImage.image.scale 
             orientation:NO]; 

    // bieberImage.transform = CGAffineTransformMakeRotation(1.57079633); 


    CABasicAnimation *crossFade = [CABasicAnimation animationWithKeyPath:@"contents"]; 
    crossFade.duration = 3.0; 


     crossFade.fromValue = (id)bieberImage.image.CGImage; 

     crossFade.toValue = (id)image.CGImage; 



     [self.bieberImage.layer addAnimation:crossFade forKey:@"animateContents"]; 

     } 

누구든지이 문제를 어떻게 해결할 수 있습니까?

미리 감사드립니다.

 UIImage *image = [UIImage imageWithCGImage:[UIImage imageWithContentsOfFile:imagePath].CGImage scale:bieberImage.image.scale 
            orientation:NO]; 

으로 :

 UIImage *image = [UIImage imageWithCGImage:[UIImage imageWithContentsOfFile:imagePath].CGImage scale:bieberImage.image.scale 
            orientation:UIImageOrientationRight]; 

편집 :

답변

16

를 교체 또는 노력이 : 난 이미 시도했지만 여전히 카메라 이미지오고되지 않습니다

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

    UIImageOrientation orientation = src.imageOrientation; 

    UIGraphicsBeginImageContext(src.size); 

    [src drawAtPoint:CGPointMake(0, 0)]; 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    if (orientation == UIImageOrientationRight) { 
     CGContextRotateCTM (context, [self radians:90]); 
    } else if (orientation == UIImageOrientationLeft) { 
     CGContextRotateCTM (context, [self radians:90]); 
    } else if (orientation == UIImageOrientationDown) { 
     // NOTHING 
    } else if (orientation == UIImageOrientationUp) { 
     CGContextRotateCTM (context, [self radians:0]); 
    } 

     return UIGraphicsGetImageFromCurrentImageContext(); 
} 

- (CGFloat) radians:(int)degrees { 
    return (degrees/180)*(22/7); 
    } 
+0

올바른 방향으로. – ishhhh

+0

다른 대안이 있습니다 – ishhhh

+0

위의 수정을 참조하십시오 - 이미지를 회전해야합니다. – Rayfleck

1
-(UIImage *)update4:(UIImage *)image { 

CGImageRef imageRef = [image CGImage]; 
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef); 
CGColorSpaceRef colorSpaceInfo = CGColorSpaceCreateDeviceRGB(); 

if (alphaInfo == kCGImageAlphaNone) 
    alphaInfo = kCGImageAlphaNoneSkipLast; 

int width, height; 

width = image.size.width; 
height = image.size.height; 

if(width>320) 
{ 

    width = 320; 
    height = height * 320/width; 
} 

if(height>480) 
{ 
    height = 480; 
    width = width * 480/height; 
} 

CGContextRef bitmap; 

if (image.imageOrientation == UIImageOrientationUp | image.imageOrientation == UIImageOrientationDown) { 
    bitmap = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, alphaInfo); 

} else { 
    bitmap = CGBitmapContextCreate(NULL, height, width, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, alphaInfo); 

} 


if (image.imageOrientation == UIImageOrientationLeft) { 
    //NSLog(@"image orientation left"); 
    CGContextRotateCTM (bitmap, 3.14/180*90); 
    CGContextTranslateCTM (bitmap, 0, -height); 

} else if (image.imageOrientation == UIImageOrientationRight) { 
    //NSLog(@"image orientation right"); 
    CGContextRotateCTM (bitmap, -3.14/180*90); 
    CGContextTranslateCTM (bitmap, -width, 0); 

} else if (image.imageOrientation == UIImageOrientationUp) { 
    //NSLog(@"image orientation up"); 

} else if (image.imageOrientation == UIImageOrientationDown) { 
    //NSLog(@"image orientation down"); 
    CGContextTranslateCTM (bitmap, width,height); 
    CGContextRotateCTM (bitmap, -3.14/180*180); 

} 




CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef); 
CGImageRef ref = CGBitmapContextCreateImage(bitmap); 
UIImage *result = [UIImage imageWithCGImage:ref]; 

CGContextRelease(bitmap); 
CGImageRelease(ref); 

return result; 
    } 


-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo { 

UIImage *updateImage = [self update4:image]; 
} 
+0

Pls이 방법을 시도하십시오 – Amit

+0

답변 주셔서 감사합니다 – ishhhh

관련 문제