【问题标题】:UIImage orientation changed when drawing in UIGraphicsBeginImageContext在 UIGraphicsBeginImageContext 中绘制时 UIImage 方向发生了变化
【发布时间】:2016-12-02 21:32:12
【问题描述】:

我想调整 UIImage 的大小,所以我使用了下面的代码。

UIGraphicsBeginImageContext(size);
[sourceImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

以上代码工作正常,但 destImage 的方向发生了变化。我不知道怎么做。

source.imageOrientation = 3
destImage.imageOrientation = 0

谁能告诉我如何调整与 sourceImage 相同方向的图像大小?

已编辑:

UIImage *scaleImage = [UIImage imageWithCGImage:destImage.CGImage
                                scale:sourceImage.scale orientation:sourceImage.imageOrientation];

已编辑:

我在调整图像大小后得到 结果。

我也试过上面的代码。但它仍然给了我相反的方向。

【问题讨论】:

  • Ekata 尝试其他解决方案
  • @fishinear 我使用了那个代码,但它不起作用,这就是我问这个问题的原因。
  • 如果你想让人们帮助你,你需要做得比“它没有用”更好。这对我来说可以。在您的问题中明确说明您使用了哪些代码、您期望它做什么以及它做了什么。
  • 另一个问题:为什么要目标图片的imageOrientation属性和原图一样? imageOrientation 说明了在绘制过程中必须如何旋转图像,这正是您执行 drawInRect 时发生的情况。所以目的地的 imageOrientation 为 0 实际上是正确的。

标签: objective-c uiimage image-resizing uigraphicscontext


【解决方案1】:

不需要为目标图像设置orientationModeorientationMode 指示如何在绘制图像时旋转图像。也就是说,UIImage 包含原始的、未旋转的数据(通常采用CGImage 格式)和orientationMode 属性。在绘制UIImage 时(例如将其添加到UIImageView 时),它也会根据需要旋转它。

您的代码也在绘制图像,因此会旋转它:

[sourceImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();

destImage 将包含旋转后的原始数据,orientationMode UIImageOrientationUp。最终结果是,绘制destImage 将产生与sourceImage 具有相同方向的相同图像。

唯一的区别是当您实际使用来自每个的CGImage 原始数据(因此忽略orientationMode 属性)时,目标图像将是源图像的旋转版本。如果你无论如何都要使用CGImage,那么最好使用CGImage级别的操作来调整图像大小,例如(这个来自http://fingertwister.tumblr.com/post/9074775714/code-to-create-a-resized-cgimage-from-a-cgimage,我没有验证它):

+ (CGImageRef)resizeCGImage:(CGImageRef)image toWidth:(int)width andHeight:(int)height {
    // create context, keeping original image properties
    CGColorSpaceRef colorspace = CGImageGetColorSpace(image);
    CGContextRef context = CGBitmapContextCreate(NULL, width, height,
        CGImageGetBitsPerComponent(image),
        CGImageGetBytesPerRow(image),
        colorspace,
        CGImageGetAlphaInfo(image));
    CGColorSpaceRelease(colorspace);

    if(context == NULL)
        return nil;

    // draw image to context (resizing it)
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
    // extract resulting image from context
    CGImageRef imgRef = CGBitmapContextCreateImage(context);
    CGContextRelease(context);

    return imgRef;
}

【讨论】:

  • 好的,谢谢。让我试试。
【解决方案2】:

调整图像视图的大小

CALayer *layer;
layer=self.view.layer;
UIGraphicsBeginImageContext(self.view.bounds.size);
CGContextClipToRect (UIGraphicsGetCurrentContext(),captureFrame); //Give your frame of image here(x,y,width,height)
[layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

也可以试试下面的代码

UIGraphicsBeginImageContext(self.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 3.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

【讨论】:

  • 感谢您的回复。让我试试这个解决方案。
  • 你能解释一下“captureFrame”吗?需要传递源图像的帧吗?
  • 是的,把你的源框架传给它
  • 好的。但是你确定它会调整图像大小吗?我应该如何知道图像已正确调整大小?
  • 是的,我相信它会调整你的图片大小
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-08
相关资源
最近更新 更多