【问题标题】:Cropping image let the picture rotate left裁剪图像让图片向左旋转
【发布时间】:2013-04-14 09:11:07
【问题描述】:

我正在开发一个应用来裁剪图像。 我正在使用下面的代码来调用裁剪的方法:

UIImage* croppedImage = [self imageCrop:imageView toRect:CGRectMake(10.0, 50.0, 320, 100)];

这里是方法的代码:

{
    //create a context to do our clipping in
    CGRect newRect = CGRectApplyAffineTransform(rect, imageViewToCrop.transform);
    UIImage *imageToCrop = imageViewToCrop.image;
    UIGraphicsBeginImageContext(newRect.size);
    CGContextRef currentContext = UIGraphicsGetCurrentContext();

    //create a rect with the size we want to crop the image to
    //the X and Y here are zero so we start at the beginning of our
    //newly created context
    CGRect clippedRect = CGRectMake(0, 0, rect.size.width, rect.size.height);
    CGContextClipToRect( currentContext, clippedRect);

    //draw the image to our clipped context using our offset rect
    CGContextDrawImage(currentContext, newRect, imageToCrop.CGImage);

    //pull the image from our cropped context
    UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext();

    //pop the context to get back to the default
    UIGraphicsEndImageContext();

    return cropped;
}

问题是当我将此图像设置为 UIImageView 时,我发现返回的图像向左旋转。我不能让它旋转起来。 有谁知道问题所在?

【问题讨论】:

    标签: iphone ios objective-c xcode crop


    【解决方案1】:

    Uiimage 有一个 imageOrientation 属性,在某些情况下指示系统应该显示图像位的方式。尝试记录该值并查看。如果它像我怀疑的那样,那么您可能需要调整您的裁剪算法,首先创建一个 CGImage,然后使用 UIImage 方法从中制作一个具有方向参数的 UIImage。

    【讨论】:

      【解决方案2】:

      正如大卫所写,问题很可能是您没有保留 imageOrientation 属性。您的代码的问题还在于您没有保留 scale 属性。 尝试像这样创建裁剪后的图像:

      CGImageRef croppedRef = CGBitmapContextCreateImage(currentContext);
      UIImage* cropped = [UIImage imageWithCGImage:croppedRef scale:imageToCrop.scale orientation:imageToCrop.imageOrientation];
      CGImageRelease(croppedRef);
      

      【讨论】:

      • 嗨,当我这样做时,我的旋转仍然关闭。你能想到使用 imageOrientation 不能正常工作的原因吗?
      猜你喜欢
      • 2015-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-05
      相关资源
      最近更新 更多