Ios 为什么我的UIImage在编辑像素后会旋转?
我搜索了SO和一些web教程,以获得这个编辑UIImage像素的Ios 为什么我的UIImage在编辑像素后会旋转?,ios,objective-c,uiimage,Ios,Objective C,Uiimage,我搜索了SO和一些web教程,以获得这个编辑UIImage像素的iAction。编辑它应该使照片变灰,但它也会旋转图像,我不知道为什么。其他人能找出原因吗?谢谢 - (IBAction)grayscale:(id)sender { CGContextRef ctx; CGImageRef imageRef = [self.workingImage CGImage]; NSUInteger width = CGImageGetWidth(imageRef); NSU
iAction
。编辑它应该使照片变灰,但它也会旋转图像,我不知道为什么。其他人能找出原因吗?谢谢
- (IBAction)grayscale:(id)sender {
CGContextRef ctx;
CGImageRef imageRef = [self.workingImage CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
_text1.text = [NSString stringWithFormat: @"%d", width];
_text2.text = [NSString stringWithFormat: @"%d", height];
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = malloc(height * width * 4);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
int byteIndex = 0;
for (int ii = 0 ; ii < width * height ; ++ii)
{
int outputColor = (rawData[byteIndex] + rawData[byteIndex+1] +
rawData[byteIndex+2]) / 3;
rawData[byteIndex] = (char) (outputColor);
rawData[byteIndex+1] = (char) (outputColor);
rawData[byteIndex+2] = (char) (outputColor);
byteIndex += 4;
}
ctx = CGBitmapContextCreate(rawData,
CGImageGetWidth( imageRef ),
CGImageGetHeight( imageRef ),
8,
CGImageGetBytesPerRow( imageRef ),
CGImageGetColorSpace( imageRef ),
kCGImageAlphaPremultipliedLast );
imageRef = CGBitmapContextCreateImage (ctx);
UIImage* rawImage = [UIImage imageWithCGImage:imageRef];
CGContextRelease(ctx);
self.workingImage = rawImage;
[_theImage setImage:_workingImage];
free(rawData);
}
-(iAction)灰度:(id)发送方{
CGContextRef-ctx;
CGImageRef imageRef=[self.workingImage CGImage];
NSU整数宽度=CGImageGetWidth(imageRef);
NSU整数高度=CGImageGetHeight(imageRef);
_text1.text=[NSString stringWithFormat:@“%d”,宽度];
_text2.text=[NSString stringWithFormat:@“%d”,高度];
CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
无符号字符*rawData=malloc(高度*宽度*4);
NSU整数字节/像素=4;
NSUInteger bytesPerRow=bytesPerPixel*宽度;
NSU整数比特分量=8;
CGContextRef context=CGBitmapContextCreate(原始数据、宽度、高度、,
bitsPerComponent、bytesPerRow、颜色空间、,
KCGIMAGEAlphaPremultipledLast | kCGBitmapByteOrder32Big);
CGCOLORSPACTERELEASE(色彩空间);
CGContextDrawImage(上下文,CGRectMake(0,0,宽度,高度),imageRef);
CGContextRelease(上下文);
int byteIndex=0;
对于(int ii=0;ii<宽度*高度;++ii)
{
int outputColor=(原始数据[byteIndex]+原始数据[byteIndex+1]+
原始数据[byteIndex+2])/3;
rawData[byteIndex]=(字符)(输出颜色);
rawData[byteIndex+1]=(字符)(输出颜色);
rawData[byteIndex+2]=(字符)(输出颜色);
byteIndex+=4;
}
ctx=CGBitmapContextCreate(原始数据,
CGImageGetWidth(imageRef),
CGImageGetHeight(imageRef),
8.
CGImageGetBytesPerRow(imageRef),
CGImageGetColorSpace(imageRef),
KCGIMAGEAlphaPremultipledLast);
imageRef=CGBitmapContextCreateImage(ctx);
UIImage*rawImage=[UIImage imageWithCGImage:imageRef];
CGContextRelease(ctx);
self.workingImage=原始图像;
[_theimagesetimage:_workingImage];
免费(原始数据);
}
我就是这样“修复”图像方向的:
- (UIImage*) fixImageOrientation:(UIImage *)image {
UIImageOrientation orient = image.imageOrientation;
CGImageRef imgRef = image.CGImage;
CGFloat aWidth = CGImageGetWidth(imgRef);;
CGFloat aHeight = CGImageGetHeight(imgRef);
CGAffineTransform transform = CGAffineTransformIdentity;
CGRect bounds = CGRectMake(0, 0, aWidth, aHeight);
CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
CGFloat boundHeight;
switch(orient)
{
case UIImageOrientationUp: //EXIF = 1
transform = CGAffineTransformIdentity;
break;
case UIImageOrientationUpMirrored: //EXIF = 2
transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
transform = CGAffineTransformScale(transform, -1.0, 1.0);
break;
case UIImageOrientationDown: //EXIF = 3
transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
transform = CGAffineTransformRotate(transform, M_PI);
break;
case UIImageOrientationDownMirrored: //EXIF = 4
transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
transform = CGAffineTransformScale(transform, 1.0, -1.0);
break;
case UIImageOrientationLeftMirrored: //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;
case UIImageOrientationLeft: //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;
case UIImageOrientationRightMirrored: //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;
case UIImageOrientationRight: //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:
NSLog(@"Invalid image orientation");
}
UIGraphicsBeginImageContext(bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft)
{
CGContextTranslateCTM(context, -aHeight, 0);
}
else
{
CGContextTranslateCTM(context, 0, -aHeight);
}
CGContextConcatCTM(context, transform);
CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, aWidth, aHeight), imgRef);
UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return imageCopy;
}
HTH检查图像方向:UIImageOrientation=image.imageOrientation;如果不是1,你可能想先旋转它。谢谢你的建议。。。东方=1!你能告诉我如何旋转它吗?我试着用
imageView.transform=CGAffineTransformMakeRotation(3.14/2)来定向UIImageView代码>,这将以正确的方向显示图片,但我的图片现在被压扁成横向,而不是其原始肖像!注意:你知道有比循环像素更容易创建灰度图像的方法吗?谢谢你的回答!是的,我肯定知道,这只是一个概念证明,这样我就可以自由地将其他变换算法应用于像素。