2014-02-20 27 views
0

我搜索了SO和一些网络教程,将这个IBAction放在一起,编辑了UIImage的像素。编辑它应该使照片灰度,但它也旋转图像,我不明白为什么。其他人能找出原因吗?谢谢!为什么编辑像素后我的UIImage旋转?

- (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); 
} 
+0

检查你的形象定位:\t UIImageOrientation东方= image.imageOrientation; 如果不是1,您可能需要先旋转它。 – Thorsten

+0

感谢您的建议... orient = 1!你能告诉我如何旋转它吗? – jake9115

+0

我试着用'imageView.transform = CGAffineTransformMakeRotation(3.14/2)来定位UIImageView; ',这显示了正确的方向图片,但我的照片现在被挤压为横向而不是其原始的肖像! – jake9115

回答

0

这就是我如何 “修复” 的形象定位:

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

+0

P.S .:你知道有更简单的方法来创建灰度图像比循环像素? http://stackoverflow.com/questions/11403939/making-a-uiimageview-grey – Thorsten

+0

谢谢你的回答!是的,我清楚地意识到,这只是一个概念证明,所以我可以自由地将其他变形算法应用于像素。 – jake9115