Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将图像转换为B&;CGW问题上下文-iPhone开发人员_Iphone_Objective C_Camera_Ios_Cgcontext - Fatal编程技术网

将图像转换为B&;CGW问题上下文-iPhone开发人员

将图像转换为B&;CGW问题上下文-iPhone开发人员,iphone,objective-c,camera,ios,cgcontext,Iphone,Objective C,Camera,Ios,Cgcontext,我正在使用一种方法将相机拍摄的图像转换为黑白。 我遇到的问题是,如果图片是在纵向模式下拍摄的,它会在下一个视图中旋转和拉伸。但是,如果是在风景,它只是找到 我只能在将图像转换为黑白时复制此错误。 这是我正在使用的方法 我不是100%认为这个bug是由转换引起的,但它只有在转换后才会发生。 哦,是的。这是iOS 4 /// - (UIImage *)convertImageToGrayScale:(UIImage *)image { // Create image rectangle wit

我正在使用一种方法将相机拍摄的图像转换为黑白。 我遇到的问题是,如果图片是在纵向模式下拍摄的,它会在下一个视图中旋转和拉伸。但是,如果是在风景,它只是找到

我只能在将图像转换为黑白时复制此错误。 这是我正在使用的方法

我不是100%认为这个bug是由转换引起的,但它只有在转换后才会发生。 哦,是的。这是iOS 4

///

- (UIImage *)convertImageToGrayScale:(UIImage *)image
{

 // Create image rectangle with current image width/height
 CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);

 // Grayscale color space
 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();

 // Create bitmap content with current image size and grayscale colorspace
 CGContextRef context = CGBitmapContextCreate(nil, image.size.width, image.size.height, 8, 0, colorSpace, kCGImageAlphaNone);

 // Draw image into current context, with specified rectangle
 // using previously defined context (with grayscale colorspace)
 CGContextDrawImage(context, imageRect, [image CGImage]);

 // Create bitmap image info from pixel data in current context
 CGImageRef imageRef = CGBitmapContextCreateImage(context);

 // Create a new UIImage object  
 UIImage *newImage = [UIImage imageWithCGImage:imageRef];

 // Release colorspace, context and bitmap information
 CGColorSpaceRelease(colorSpace);
 CGContextRelease(context);
 CFRelease(imageRef);

 // Return the new grayscale image
 return newImage;
}

@lessfame

我想我明白了。 现在去测试

事实证明,存在UIImage方向标志。 我在以下位置找到了一个支持线程:

我的想法是使用一个开关来检查方向,并根据这个方向将其旋转到正确的方向

我弄明白密码后再发

  • @莱斯法姆
    • K我知道了

      我最后做的是找到这个函数,它根据变换的方向旋转图像。它所做的是修复这个奇怪的旋转错误

      下面是代码片段:

      - (UIImage *)rotateImage:(UIImage *)image {
      
      int kMaxResolution = 320; // Or whatever
      
      CGImageRef imgRef = image.CGImage;
      
      CGFloat width = CGImageGetWidth(imgRef);
      CGFloat height = CGImageGetHeight(imgRef);
      
      
      CGAffineTransform transform = CGAffineTransformIdentity;
      CGRect bounds = CGRectMake(0, 0, width, height);
      if (width > kMaxResolution || height > kMaxResolution) {
          CGFloat ratio = width/height;
          if (ratio > 1) {
              bounds.size.width = kMaxResolution;
              bounds.size.height = bounds.size.width / ratio;
          }
          else {
              bounds.size.height = kMaxResolution;
              bounds.size.width = bounds.size.height * ratio;
          }
      }
      
      CGFloat scaleRatio = bounds.size.width / width;
      CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
      CGFloat boundHeight;
      UIImageOrientation orient = image.imageOrientation;
      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:
              [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];
      
      }
      
      UIGraphicsBeginImageContext(bounds.size);
      
      CGContextRef context = UIGraphicsGetCurrentContext();
      
      if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) {
          CGContextScaleCTM(context, -scaleRatio, scaleRatio);
          CGContextTranslateCTM(context, -height, 0);
      }
      else {
          CGContextScaleCTM(context, scaleRatio, -scaleRatio);
          CGContextTranslateCTM(context, 0, -height);
      }
      
      CGContextConcatCTM(context, transform);
      
      CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
      UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
      
      return imageCopy; }
      
      该方法返回一个图像,然后我可以在黑白函数中使用它

      - (UIImage *)convertImageToGrayScale:(UIImage *)image {
      
      image = [self rotateImage:image];//THIS IS WHERE REPAIR THE ROTATION PROBLEM
      
      // Create image rectangle with current image width/height
      CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);
      
      // Grayscale color space
      CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
      
      // Create bitmap content with current image size and grayscale colorspace
      CGContextRef context = CGBitmapContextCreate(nil, image.size.width, image.size.height, 8, 0, colorSpace, kCGImageAlphaNone);
      
      // Draw image into current context, with specified rectangle
      // using previously defined context (with grayscale colorspace)
      CGContextDrawImage(context, imageRect, [image CGImage]);
      
      // Create bitmap image info from pixel data in current context
      CGImageRef imageRef = CGBitmapContextCreateImage(context);
      
      // Create a new UIImage object  
      UIImage *newImage = [UIImage imageWithCGImage:imageRef];
      
      // Release colorspace, context and bitmap information
      CGColorSpaceRelease(colorSpace);
      CGContextRelease(context);
      CFRelease(imageRef);
      
      // Return the new grayscale image
      return newImage; }
      

      享受吧

      I经过修改,能够在不降低分辨率的情况下处理2.0缩放图像:

      - (UIImage *)convertImageToGrayScale:(UIImage *)image {
          // Create image rectangle with current image width/height
          CGRect imageRect = CGRectMake(0, 0, image.scale*image.size.width, image.scale*image.size.height);
      
          // Grayscale color space
          CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
      
          // Create bitmap content with current image size and grayscale colorspace
          CGContextRef context = CGBitmapContextCreate(nil, image.scale*image.size.width, image.scale*image.size.height, 8, 0, colorSpace, kCGImageAlphaNone);
      
          // Draw image into current context, with specified rectangle
          // using previously defined context (with grayscale colorspace)
          CGContextDrawImage(context, imageRect, [image CGImage]);
      
          // Create bitmap image info from pixel data in current context
          CGImageRef imageRef = CGBitmapContextCreateImage(context);
      
          // Create a new UIImage object
          UIImage *newImage = [UIImage imageWithCGImage:imageRef scale:image.scale orientation:image.imageOrientation];
      
          // Release colorspace, context and bitmap information
          CGColorSpaceRelease(colorSpace);
          CGContextRelease(context);
          CFRelease(imageRef);
      
          // Return the new grayscale image
          return newImage;
      }
      

      请格式化您的代码…无法读取!???代码在我看来是格式化的。如何使其具有可伸缩性?