2011-11-30 73 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"]; 

     } 

誰能告訴我如何糾正這個問題。

在此先感謝。

回答

16

替換:

 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]; 

編輯: 或者試試這個:

- (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

請試試這個方法 – Amit

+0

感謝您的回答 – ishhhh