- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我关注了this example 从 Apple 缩小非常大的图像我是 downloading from a remote location .我已经用 Swift 重写了代码。它显然有效,但是当我调用 MTKTextureLoader.newTexture
时,应用程序崩溃并在 _loadCGImage
中出现 EXC_BAD_ACCESS
。没有其他提示,但我怀疑图像数据已经发布或其他...
任何提示为什么它会在没有任何正确错误消息的情况下崩溃?
这是顶层代码,
// this is an extension of MTKTextureLoader
// [...]
if let uiImage = UIImage(contentsOfFile: cachedFileURL.path) {
let maxDim : CGFloat = 8192
if uiImage.size.width > maxDim || uiImage.size.height > maxDim {
let scale = uiImage.size.width > maxDim ? maxDim / uiImage.size.width : maxDim / uiImage.size.height
if let cgImage = MTKTextureLoader.downsize(image: uiImage, scale: scale) {
return self.newTexture(with: cgImage, options: options, completionHandler: completionHandler)
} else {
anError = TextureError.CouldNotDownsample
}
} else {
return self.newTexture(withContentsOf: cachedFileURL, options: options, completionHandler: completionHandler)
}
}
这是缩小尺寸的方法,
private static func downsize(image: UIImage, scale: CGFloat) -> CGImage? {
let destResolution = CGSize(width: Int(image.size.width * scale), height: Int(image.size.height * scale))
let kSourceImageTileSizeMB : CGFloat = 40.0 // The tile size will be (x)MB of uncompressed image data
let pixelsPerMB = 262144
let tileTotalPixels = kSourceImageTileSizeMB * CGFloat(pixelsPerMB)
let destSeemOverlap : CGFloat = 2.0 // the numbers of pixels to overlap the seems where tiles meet.
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
guard let destContext = CGContext(data: nil, width: Int(destResolution.width), height: Int(destResolution.height), bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: bitmapInfo.rawValue) else {
NSLog("failed to create the output bitmap context!")
return nil
}
var sourceTile = CGRect()
sourceTile.size.width = image.size.width
sourceTile.size.height = floor( tileTotalPixels / sourceTile.size.width )
print("source tile size: \(sourceTile.size)")
sourceTile.origin.x = 0.0
// the output tile is the same proportions as the input tile, but
// scaled to image scale.
var destTile = CGRect()
destTile.size.width = destResolution.width
destTile.size.height = sourceTile.size.height * scale
destTile.origin.x = 0.0
print("dest tile size: \(destTile.size)")
// the source seem overlap is proportionate to the destination seem overlap.
// this is the amount of pixels to overlap each tile as we assemble the output image.
let sourceSeemOverlap : CGFloat = floor( ( destSeemOverlap / destResolution.height ) * image.size.height )
print("dest seem overlap: \(destSeemOverlap), source seem overlap: \(sourceSeemOverlap)")
// calculate the number of read/write operations required to assemble the
// output image.
var iterations = Int( image.size.height / sourceTile.size.height )
// if tile height doesn't divide the image height evenly, add another iteration
// to account for the remaining pixels.
let remainder = Int(image.size.height) % Int(sourceTile.size.height)
if remainder > 0 {
iterations += 1
}
// add seem overlaps to the tiles, but save the original tile height for y coordinate calculations.
let sourceTileHeightMinusOverlap = sourceTile.size.height
sourceTile.size.height += sourceSeemOverlap
destTile.size.height += destSeemOverlap
print("beginning downsize. iterations: \(iterations), tile height: \(sourceTile.size.height), remainder height: \(remainder)")
for y in 0..<iterations {
// create an autorelease pool to catch calls to -autorelease made within the downsize loop.
autoreleasepool {
print("iteration \(y+1) of \(iterations)")
sourceTile.origin.y = CGFloat(y) * sourceTileHeightMinusOverlap + sourceSeemOverlap
destTile.origin.y = ( destResolution.height ) - ( CGFloat( y + 1 ) * sourceTileHeightMinusOverlap * scale + destSeemOverlap )
// create a reference to the source image with its context clipped to the argument rect.
if let sourceTileImage = image.cgImage?.cropping( to: sourceTile ) {
// if this is the last tile, its size may be smaller than the source tile height.
// adjust the dest tile size to account for that difference.
if y == iterations - 1 && remainder > 0 {
var dify = destTile.size.height
destTile.size.height = CGFloat( sourceTileImage.height ) * scale
dify -= destTile.size.height
destTile.origin.y += dify
}
// read and write a tile sized portion of pixels from the input image to the output image.
destContext.draw(sourceTileImage, in: destTile, byTiling: false)
}
// !!! In the original LargeImageDownsizing code, it released the source image here !!!
// [image release];
// !!! I assume I don't need to do that in Swift?? !!!
/* while CGImageCreateWithImageInRect lazily loads just the image data defined by the argument rect,
that data is finally decoded from disk to mem when CGContextDrawImage is called. sourceTileImageRef
maintains internally a reference to the original image, and that original image both, houses and
caches that portion of decoded mem. Thus the following call to release the source image. */
// http://en.swifter.tips/autoreleasepool/
// drain will be called
// to free all objects that were sent -autorelease within the scope of this loop.
}
// !!! Original code reallocated the image here !!!
// we reallocate the source image after the pool is drained since UIImage -imageNamed
// returns us an autoreleased object.
}
print("downsize complete.")
// create a CGImage from the offscreen image context
return destContext.makeImage()
}
编辑:
每次我尝试用 CGImage
初始化 MTLTexture
时它都会崩溃,即使图像很小并且适合内存。所以它似乎与调整大小无关......这段代码也会崩溃,
func newTexture(with uiImage: UIImage, options: [String : NSObject]? = nil, completionHandler: @escaping MTKTextureLoaderCallback) {
if let cgImage = uiImage.cgImage {
return self.newTexture(with: cgImage, options: options, completionHandler: completionHandler)
} else {
completionHandler(nil, TextureError.CouldNotBeCreated)
}
}
在 ImageIO copyImageBlockSetWithOptions
。
编辑2:基于 warrenm 答案的解决方法:调用 newTexture(with: cgImage)
同步而不是异步。例如,上面的函数变成了,
func newTexture(with uiImage: UIImage, options: [String : NSObject]? = nil, completionHandler: MTKTextureLoaderCallback) {
if let cgImage = uiImage.cgImage {
let tex = try? self.newTexture(with: cgImage, options: options)
completionHandler(tex, tex == nil ? TextureError.CouldNotBeCreated : nil)
} else {
completionHandler(nil, TextureError.CouldNotGetCGImage)
}
}
(请注意,我已经删除了 @escaping
,因为现在我实际上调用了 sync 方法。)
缩小代码是正确的。它现在可以使用此解决方法:)
最佳答案
这似乎是用于创建纹理的 CGImage
生命周期的问题,也可能是 MetalKit 中的错误。
本质上,上下文返回的 CGImage
只能保证在创建它的自动释放池范围结束之前一直有效。当您调用异步 newTexture
方法时,MTKTextureLoader
将创建纹理的工作移至后台线程,并在 CGImage
外部进行操作在其后备存储已被释放后,其封闭自动释放池的范围。
您可以通过在完成处理程序中捕获图像(这将导致 ARC 延长其生命周期)或使用相应的同步纹理创建方法 newTexture(with:options:)
,它将保留在相关范围内,直到纹理完全初始化。
关于ios - 将非常大的图像缩小到 MTLTexture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42567140/
我正在尝试学习 Knockout 并尝试创建一个照片 uploader 。我已成功将一些图像存储在数组中。现在我想回帖。在我的 knockout 码(Javascript)中,我这样做: 我在 Jav
我正在使用 php 编写脚本。我的典型问题是如何在 mysql 中添加一个有很多替代文本和图像的问题。想象一下有机化学中具有苯结构的描述。 最有效的方法是什么?据我所知,如果我有一个图像,我可以在数据
我在两个图像之间有一个按钮,我想将按钮居中到图像高度。有人可以帮帮我吗? Entrar
下面的代码示例可以在这里查看 - http://dev.touch-akl.com/celebtrations/ 我一直在尝试做的是在 Canvas 上绘制 2 个图像(发光,然后耀斑。这些图像的链接
请检查此https://jsfiddle.net/rhbwpn19/4/ 图像预览对于第一篇帖子工作正常,但对于其他帖子则不然。 我应该在这里改变什么? function readURL(input)
我对 Canvas 有疑问。我可以用单个图像绘制 Canvas ,但我不能用单独的图像绘制每个 Canvas 。- 如果数据只有一个图像,它工作正常,但数据有多个图像,它不工作你能帮帮我吗? va
我的问题很简单。如何获取 UIImage 的扩展类型?我只能将图像作为 UIImage 而不是它的名称。图像可以是静态的,也可以从手机图库甚至文件路径中获取。如果有人可以为此提供一点帮助,将不胜感激。
我有一个包含 67 个独立路径的 SVG 图像。 是否有任何库/教程可以为每个路径创建单独的光栅图像(例如 PNG),并可能根据路径 ID 命名它们? 最佳答案 谢谢大家。我最终使用了两个答案的组合。
我想将鼠标悬停在一张图片(音乐专辑)上,然后播放一张唱片,所以我希望它向右移动并旋转一点,当它悬停时我希望它恢复正常动画片。它已经可以向右移动,但我无法让它随之旋转。我喜欢让它尽可能简单,因为我不是编
Retina iOS 设备不显示@2X 图像,它显示 1X 图像。 我正在使用 Xcode 4.2.1 Build 4D502,该应用程序的目标是 iOS 5。 我创建了一个测试应用(主/细节)并添加
我正在尝试从头开始以 Angular 实现图像 slider ,并尝试复制 w3school基于图像 slider 。 下面我尝试用 Angular 实现,谁能指导我如何使用 Angular 实现?
我正在尝试获取图像的图像数据,其中 w= 图像宽度,h = 图像高度 for (int i = x; i imageData[pos]>0) //Taking data (here is the pr
我的网页最初通过在 javascript 中动态创建图像填充了大约 1000 个缩略图。由于权限问题,我迁移到 suPHP。现在不用标准 标签本身 我正在通过这个 php 脚本进行检索 $file
我正在尝试将 python opencv 图像转换为 QPixmap。 我按照指示显示Page Link我的代码附在下面 img = cv2.imread('test.png')[:,:,::1]/2
我试图在这个 Repository 中找出语义分割数据集的 NYU-v2 . 我很难理解图像标签是如何存储的。 例如,给定以下图像: 对应的标签图片为: 现在,如果我在 OpenCV 中打开标签图像,
import java.util.Random; class svg{ public static void main(String[] args){ String f="\"
我有一张 8x8 的图片。 (位图 - 可以更改) 我想做的是能够绘制一个形状,给定一个 Path 和 Paint 对象到我的 SurfaceView 上。 目前我所能做的就是用纯色填充形状。我怎样才
要在页面上显示图像,你需要使用源属性(src)。src 指 source 。源属性的值是图像的 URL 地址。 定义图像的语法是: 在浏览器无法载入图像时,替换文本属性告诉读者她们失去的信息。此
**MMEditing是基于PyTorch的图像&视频编辑开源工具箱,支持图像和视频超分辨率(super-resolution)、图像修复(inpainting)、图像抠图(matting)、
我正在尝试通过资源文件将图像插入到我的程序中,如下所示: green.png other files 当我尝试使用 QImage 或 QPixm
我是一名优秀的程序员,十分优秀!