2011-11-30 69 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