我正在开发一个照片应用程序。
这里我使用的是照片库中的图像以及通过相机拍摄并在图像视图中显示的图像。
照片库中的图像方向正确,但相机图像的显示方向不同。
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"];
}
任何人都可以告诉我如何纠正这个问题。
提前致谢。
答案 0 :(得分: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);
}
答案 1 :(得分: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];
}