- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
试图弄清楚当一个人得到 cgImage
时图像是如何旋转或翻转的来自 UIImage
这是在肖像模式。
即如果我有案例,image.size.width < image.size.height
然后调用let cg = image.cgImage!
, 我得到 cg.width > cg.height
(实际上,cg.width == image.size.height && cg.height == image.size.width
)。我知道 CGImage
的坐标差异和 UIImage
, 但我不明白 - UIImage
的哪个角落被视为 CGImage
的来源图像是否以某种方式翻转?
这弄乱了我的裁剪代码,我在其中简单地计算了 UIImage
的裁剪矩形。但随后尝试通过调用 image.cgImage!.cropping(to: rect)
来裁剪图像给了我意想不到的结果(裁剪了错误的区域)。是否rect
需要在 CGImage
的坐标系?我试着像这样翻转它,但它也无济于事:
swap(&croppingRect.origin.x, &croppingRect.origin.y)
swap(&croppingRect.size.width, &croppingRect.size.height)
最佳答案
对于那些可能遇到与我类似问题的人,我发布了这个 Swift playground 代码,它帮助我了解发生了什么以及如何解决它:
import UIKit
let image : UIImage = UIImage(named: "down.JPG")!
image.size
image.imageOrientation.rawValue
let center = CGPoint(x: 0.3, y: 0.3)
let kx = (center.x > 0.5 ? (1-center.x) : center.x)
let ky = (center.y > 0.5 ? (1-center.y) : center.y)
let size = CGSize(width: image.size.width*kx, height: image.size.height*ky)
// cropRect is defined in UIImage coordinate system
// it is defined as a rectangle with center in "center", with proportions
// to the original image. size of rectangle is defined by the distance from
// the center to the nearest edge divided by 2
// this was chosen due to my specific needs and can be adjusted at will
// (just don't exceed limits of the original image)
var cropRect = CGRect(x: center.x*image.size.width-size.width/2,
y: center.y*image.size.height-size.height/2,
width: size.width,
height: size.height)
cropRect
if self.imageOrientation == .right || self.imageOrientation == .down || self.imageOrientation == .left
{
let k : CGFloat = (self.imageOrientation == .right ? -1 :(self.imageOrientation == .left ? 1 : 2))
let dy = (self.imageOrientation == .right ? self.size.width : (self.imageOrientation == .left ? 0 :self.size.height))
let dx = (self.imageOrientation == .down ? self.size.width : (self.imageOrientation == .left ? self.size.height : 0))
let rotate = CGAffineTransform(rotationAngle: k*90/180*CGFloat.pi)
let translate = CGAffineTransform(translationX: dx, y: dy)
cropRect = cropRect.applying(rotate).applying(translate)
}
let cgImage = image.cgImage!
cgImage.width
cgImage.height
cropRect
let cropped = cgImage.cropping(to: cropRect)
if cropped != nil
{
cropped!.width
cropped!.height
let croppedImage = UIImage(cgImage: cropped!, scale: image.scale, orientation: image.imageOrientation)
}
为所有可能的相机配置拍摄 4 张图片:“up.JPG”、“down.JPG”、“left.JPG”和“right.JPG”,并将它们上传到 Resources 的子文件夹你的 Playground 。一个一个地加载它们并检查参数发生了什么。此代码帮助我提出了可行的解决方案:当图像具有 .right
或 .down
方向时,将仿射变换应用于裁剪矩形以获得所需的裁剪。
关于ios - 来自肖像 UIImage 的 CGImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43712847/
我正在尝试将 cgImage 从旧函数转换为新函数,但我不确定最后一个参数的参数应该是什么。基于旧函数的 param alphaType: 参数为 .alphaIsOne,新参数 param isOp
我尝试通过填写自己的数据来创建 UIImage。到目前为止一切正常。但是,如果我尝试多次调用此函数,它似乎会填满内存,直到应用程序崩溃。使用 VM Tracker,我发现当应用程序崩溃时,脏内存会增长
这是 Apple 的代码(来自技术问答 QA1702),用于从视频缓冲区获取 UIImage。不幸的是,返回的图像旋转了 90 度。如何编辑它以使返回的图像方向正确? - (UIImage *) im
使用标准图像格式(jpeg、gif、png 等)从文件加载 CGImage 或 NSImage 非常简单。 但是,我现在需要从使用 libfreetype 生成的内存中的字节数组创建 CGImage。
我正在使用 CIBloom 模糊 CGImage 中的一些文本,但发生的情况是 CIBloom 过滤器似乎使我的图像变得非常小 . 当滤波器内核变大时效果更差。我有点预料到这一点,但我需要一种方法来关
我有一个内存泄漏,我只是不知道如何解决。 这是泄露的代码: [newImg release]; CGColorSpaceRef d_colorSpace = CGColorSpaceCreateDev
在 iOS 4.0 及更高版本中,有没有一种方法可以在不将整个图像加载到内存的情况下对 CGImage 进行分割?我试图做的是*以编程方式*分割图像,以便在使用大图像的 CATiledLayer 应用
在我的应用中,我想生成一个视频的多个缩略图,最好是质量好的缩略图。我的旧方法是执行一个循环 15 次,并在不同的时间复制一个 CGImage。如下图 func generateThumbnails(_
一种方法是: CGImageRef scaledImage(CGImageref, destination rect) { context = CGBitmapContextCreate(re
我有一个 UIView 子类,它呈现应用了 mask 的图像。它在所有设备(仅限 iPad)上都能完美运行,除了那些具有宽色域显示屏(最新的 iPad Pro)的设备,在这些设备上, mask 呈现完
我正在为 mac 开发一个类似放大镜的应用程序。我的目标是能够在放大时精确定位单个像素。我在 mouseMoved(with event: NSEvent) 中使用此代码: let captureSi
我希望将一个 CGImage 覆盖在另一个之上。 作为示例,第一个 CGImage 为 1024x768,并且想要在给定位置覆盖第二个 100x100 CGImage。 我已经了解了如何使用 NSIm
所以我一直在开发一个小的“绘图”应用程序,用户可以在网格 Canvas 上绘制图像(基本上创建像素艺术)。我想创建一个导出此 Canvas 的函数(将其视为 UIColor 数组,然后将其转换为 Ra
有没有一种方法可以在 iOS 中将原始图像数据存储和加载到磁盘上,以节省解压时间和内存并减少应用程序的脏内存占用? 保罗·哈达德已hinting to something like this我知道这项
我有一个手绘 View (用户可以用手指画线)。我只使用几种颜色,所以我有一个我写的压缩算法(想通过本地网络将它发送到另一台 iPad)但我似乎无法准确地从图形上下文中获取数据,即使使用这个简单的测试
我正在尝试编写一段代码来判断 CGPoint 是否位于图像的黑色区域内(成功的答案是 here )。在我的实现中,如果使用此代码触摸点为黑色,我可以成功返回 NSLog: -(void)touches
我正在通过网络连接接收一系列图像,并希望尽快显示它们,最高可达 30 FPS。我派生了一个 UIView 对象(因此我可以覆盖 drawRect)并公开了一个名为 cameraImage 的 UIIm
我有以下代码: UIImage *img = [UIImage imageNamed:@"BRBlueCircleMask"]; CGImageRef activeCirleMaskImage = i
对于物理缩放任何 UIImage 的方法,我需要创建位图上下文,它具有与图像完全相同的设置,除了目标宽度和高度。 到目前为止,这是我的代码。缺少什么:如何为 CGBitmapContextCreate
我有一个图像,其 RGB 分量是全白的,具有不同的 alpha -- 例如,RGBA 格式的 0xFFFFFF09。但是,当我使用 UIImage 或 CGImage API 加载此图像,然后在 CG
我是一名优秀的程序员,十分优秀!