开发者社区> 问答> 正文

剪裁图片导致图片向左旋转

做了一个剪裁图片的程序,调用剪裁:
`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的时候,图片向左旋转了。而且我也没办法让他向上旋转,怎么办?

展开
收起
爵霸 2016-03-23 11:45:03 1889 0
1 条回答
写回答
取消 提交回答
  • 如楼上所说的可能是imageOrientation属性的问题。并且你也没有正确保存scale。这样创建剪裁的图片:

    CGImageRef croppedRef = CGBitmapContextCreateImage(currentContext);
    UIImage* cropped = [UIImage imageWithCGImage:croppedRef scale:imageToCrop.scale orientation:imageToCrop.imageOrientation];
    CGImageRelease(croppedRef);
    2019-07-17 19:10:40
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载