[6/3a1b] UIImage的Orientation
UIImage内部应该是以CGImageRef的方式进行存储的,而且无论你是竖向摄影还是横向摄影,最后储存的时候其实都是横向的,也就是UIImageOrientationUp的方式,然后以指定不同的UIImageOrientation的取值来确定显示的时候的方向。这样一来对于UIImageView,不管你是如何来take的这个photo,都可以以正确的方向显示这个Image。
这样一来当使用
CGImageCreateWithImageInRect
的时候,如果你的图片是UIImageOrientationLeft或者UIImageOrientationRight的时候,你所使用的长和宽必须互换,原因是其中的CGImageRef是横向存储的。
[UIImageInstance CGImage]的时候,UIImageOrientation的信息会丢失,所以一定要提前进行纪录,然后
在使用[UIImage imageWithCGImage: scale: orientation:]再次提供,进行存储。
我估计其中的scale信息实际上也是UIImage和CGImageRef的区别,这也就是为什么苹果要提供这个API的原因
以下是一个crop UIImage称为一个正方形的代码
//crop a rectangle into a maxium square in it +(UIImage*)cropIntoSquareFromImage:(UIImage*)image{ UIImage* squareImage; CGFloat inputImageWidth; CGFloat inputImageHeight; CGFloat inputImageOrientation=image.imageOrientation; if (inputImageOrientation==UIImageOrientationLeft||inputImageOrientation==UIImageOrientationRight) { inputImageWidth=image.size.height; inputImageHeight=image.size.width; }else{ inputImageHeight=image.size.height; inputImageWidth=image.size.width; } CGRect cropRect; CGRect intCropRect; //crop image within center if (inputImageHeight>=inputImageWidth) { cropRect=CGRectMake(0,(inputImageHeight-inputImageWidth)/2, inputImageWidth, inputImageWidth); }else{ cropRect=CGRectMake((inputImageWidth-inputImageHeight)/2, 0, inputImageHeight, inputImageHeight); } intCropRect=CGRectIntegral(cropRect); CGImageRef cropImageRef=CGImageCreateWithImageInRect([image CGImage], intCropRect); if (inputImageOrientation==UIImageOrientationLeft) { squareImage= [[UIImage alloc] initWithCGImage:cropImageRef scale:[FZDataImageController screenScale] orientation:UIImageOrientationLeft]; }else if (inputImageOrientation==UIImageOrientationRight) { squareImage= [[UIImage alloc] initWithCGImage:cropImageRef scale:[FZDataImageController screenScale] orientation:UIImageOrientationRight]; }else if (inputImageOrientation==UIImageOrientationDown) { squareImage= [[UIImage alloc] initWithCGImage:cropImageRef scale:[FZDataImageController screenScale] orientation:UIImageOrientationDown]; }else{ squareImage= [[UIImage alloc] initWithCGImage:cropImageRef]; } CFRelease(cropImageRef); return squareImage; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】