Ios 缩小iPhone摄像头的滚动图像会导致意外的瑕疵

Ios 缩小iPhone摄像头的滚动图像会导致意外的瑕疵,ios,objective-c,uiimage,cgcontext,Ios,Objective C,Uiimage,Cgcontext,我正在缩小相机的滚动图像,它们显示的是人工制品 示例图像: CGSize s = CGSizeMake(320, 320); UIImage *scaledImage = [picture resizedImageWithContentMode:contentMode bounds:s inte

我正在缩小相机的滚动图像,它们显示的是人工制品

示例图像:

     CGSize s = CGSizeMake(320, 320);
     UIImage *scaledImage = [picture resizedImageWithContentMode:contentMode
                                                     bounds:s
                                       interpolationQuality:kCGInterpolationHigh];

图像为720x960。。这是因为比例无关紧要吗

以下是我的UIImage类别:

    /**
 * Returns a copy of this image that is cropped to the given bounds.
 * The bounds will be adjusted using CGRectIntegral.
 * This method ignores the image's imageOrientation setting.
 */

    - (UIImage *)croppedImage:(CGRect)bounds {

      CGImageRef imageRef   = CGImageCreateWithImageInRect([self CGImage], bounds);
      UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];

      CGImageRelease(imageRef);

      return croppedImage;
    }

/**
 * Returns a rescaled copy of the image, taking into account its orientation
 * The image will be scaled disproportionately if necessary to fit the bounds specified by the parameter
 */

    - (UIImage *)resizedImage:(CGSize)newSize interpolationQuality:(CGInterpolationQuality)quality {

      BOOL drawTransposed;

      CGAffineTransform transform = CGAffineTransformIdentity;

      switch (self.imageOrientation) {
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
        drawTransposed = YES;
        break;
        default:
        drawTransposed = NO;
      }

      transform = [self transformForOrientation:newSize];

      return [self resizedImage:newSize
                      transform:transform
                 drawTransposed:drawTransposed
           interpolationQuality:quality];
    }

/**
 * Resizes the image according to the given content mode, taking into account 
 * the image's orientation
 */

    - (UIImage *)resizedImageWithContentMode:(UIViewContentMode)contentMode
                                      bounds:(CGSize)bounds
                        interpolationQuality:(CGInterpolationQuality)quality {

      CGFloat horizontalRatio = bounds.width / self.size.width;
      CGFloat verticalRatio   = bounds.height / self.size.height;
      CGFloat ratio;

      switch (contentMode) {
        case UIViewContentModeScaleAspectFill:
          ratio = MAX(horizontalRatio, verticalRatio);
          break;

        case UIViewContentModeScaleAspectFit:
          ratio = MIN(horizontalRatio, verticalRatio);
          break;

        default:
          [NSException raise:NSInvalidArgumentException format:@"Unsupported content mode: %d", contentMode];
      }

        ratio = MIN(horizontalRatio, verticalRatio);

      CGSize newSize = CGSizeMake(self.size.width * ratio, self.size.height * ratio);

      return [self resizedImage:newSize interpolationQuality:quality];
    }

    #pragma mark - Private helper methods

    /**
     * Returns a copy of the image that has been transformed using the given affine transform and scaled to the new size
     * The new image's orientation will be UIImageOrientationUp, regardless of the current image's orientation
     * If the new size is not integral, it will be rounded up
     */

    - (UIImage *)resizedImage:(CGSize)newSize
                    transform:(CGAffineTransform)transform
               drawTransposed:(BOOL)transpose
         interpolationQuality:(CGInterpolationQuality)quality {

      CGRect newRect        = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
      CGRect transposedRect = CGRectMake(0, 0, newRect.size.height, newRect.size.width);
      CGImageRef imageRef   = self.CGImage;

      /**
       * Fix for a colorspace / transparency issue that affects some types of
       * images. See here: http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/comment-page-2/#comment-39951
       */

      CGContextRef bitmap =CGBitmapContextCreate( NULL,
                                                 newRect.size.width,
                                                 newRect.size.height,
                                                 8,
                                                 0,
                                                 CGImageGetColorSpace( imageRef ),
                                                 kCGImageAlphaNoneSkipLast );

      // Rotate and/or flip the image if required by its orientation
      CGContextConcatCTM(bitmap, transform);

    //    CGContextSetShouldAntialias(bitmap,YES);

      // Set the quality level to use when rescaling
      CGContextSetInterpolationQuality(bitmap, quality);

      // Draw into the context; this scales the image
      CGContextDrawImage(bitmap, transpose ? transposedRect : newRect, imageRef);

      // Get the resized image from the context and a UIImage
      CGImageRef newImageRef = CGBitmapContextCreateImage(bitmap);
      UIImage *newImage = [UIImage imageWithCGImage:newImageRef];

      // Clean up
      CGContextRelease(bitmap);
      CGImageRelease(newImageRef);

      return newImage;
    }

    /**
     * Returns an affine transform that takes into account the image orientation 
     * when drawing a scaled image
     */

    - (CGAffineTransform)transformForOrientation:(CGSize)newSize {

      CGAffineTransform transform = CGAffineTransformIdentity;

      switch (self.imageOrientation) {
        case UIImageOrientationDown:           // EXIF = 3
        case UIImageOrientationDownMirrored:   // EXIF = 4
          transform = CGAffineTransformTranslate(transform, newSize.width, newSize.height);
          transform = CGAffineTransformRotate(transform, M_PI);
          break;

        case UIImageOrientationLeft:           // EXIF = 6
        case UIImageOrientationLeftMirrored:   // EXIF = 5
          transform = CGAffineTransformTranslate(transform, newSize.width, 0);
          transform = CGAffineTransformRotate(transform, M_PI_2);
          break;

        case UIImageOrientationRight:          // EXIF = 8
        case UIImageOrientationRightMirrored:  // EXIF = 7
          transform = CGAffineTransformTranslate(transform, 0, newSize.height);
          transform = CGAffineTransformRotate(transform, -M_PI_2);
          break;
        default:
          break;
      }

      switch (self.imageOrientation) {
        case UIImageOrientationUpMirrored:     // EXIF = 2
        case UIImageOrientationDownMirrored:   // EXIF = 4
          transform = CGAffineTransformTranslate(transform, newSize.width, 0);
          transform = CGAffineTransformScale(transform, -1, 1);
          break;

        case UIImageOrientationLeftMirrored:   // EXIF = 5
        case UIImageOrientationRightMirrored:  // EXIF = 7
          transform = CGAffineTransformTranslate(transform, newSize.height, 0);
          transform = CGAffineTransformScale(transform, -1, 1);
          break;
        default:
          break;
      }

      return transform;
    }

我不会处理
UIImage
自己调整大小的问题,因为图像中存在元数据部分,您可能最终无法正确处理。有一篇很好的文章(尽管它已经很老了)介绍了通过调整大小可能会出现的意外工件

我正在使用CocoaPod中的UIImage Resize库,它工作得很好


这可能不是您想要的答案,但可能会从其他角度提供信息。

发布一些前后图像,以便我们了解您所谈论的工件类型。完成。它们看起来很模糊。我希望这些人工制品不会出现,因为我正在降低分辨率。一些人工制品是通过在iPad上为iPhone应用程序使用2x功能引入的