我关注了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/
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
我有带有Logo图像的公司模型has_attached_file:logo我用他们的Logo创建了许多公司。现在,我需要添加新样式has_attached_file:logo,:styles=>{:small=>"30x15>",:medium=>"155x85>"}我是否应该重新上传所有旧数据以重新生成新样式?我不这么认为……或者有什么rake任务可以重新生成样式吗? 最佳答案 参见Thumbnail-Generation.如果rake任务不适合你,你应该能够在控制台中使用一个片段来调用重新处理!关于相关公司
1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里
我正在尝试使用Ruby2.0.0和Rails4.0.0提供的API从imgur中提取图像。我已尝试按照Ruby2.0.0文档中列出的各种方式构建http请求,但均无济于事。代码如下:require'net/http'require'net/https'defimgurheaders={"Authorization"=>"Client-ID"+my_client_id}path="/3/gallery/image/#{img_id}.json"uri=URI("https://api.imgur.com"+path)request,data=Net::HTTP::Get.new(path
2022/8/4更新支持加入水印水印必须包含透明图像,并且水印图像大小要等于原图像的大小pythonconvert_image_to_video.py-f30-mwatermark.pngim_dirout.mkv2022/6/21更新让命令行参数更加易用新的命令行使用方法pythonconvert_image_to_video.py-f30im_dirout.mkvFFMPEG命令行转换一组JPG图像到视频时,是将这组图像视为MJPG流。我需要转换一组PNG图像到视频,FFMPEG就不认了。pyav内置了ffmpeg库,不需要系统带有ffmpeg工具因此我使用ffmpeg的python包装p
Rails相对较新。我正在尝试调用一个API,它应该向我返回一个唯一的URL。我的应用程序中捆绑了HTTParty。我已经创建了一个UniqueNumberController,并且我已经阅读了几个HTTParty指南,直到我想要什么,但也许我只是有点迷路,真的不知道该怎么做。基本上,我需要做的就是调用API,获取它返回的URL,然后将该URL插入到用户的数据库中。谁能给我指出正确的方向或与我分享一些代码? 最佳答案 假设API为JSON格式并返回如下数据:{"url":"http://example.com/unique-url"
有这样的事吗?我想在Ruby程序中使用它。 最佳答案 试试这个http://csl.sublevel3.org/jp2a/此外,Imagemagick可能还有一些东西 关于ruby-是否有将图像文件转换为ASCII艺术的命令行程序或库?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/6510445/
我正在使用Dragonfly在Rails3.1应用程序上处理图像。我正在努力通过url将图像分配给模型。我有一个很好的表格:{:multipart=>true}do|f|%>RemovePicture?Dragonfly的文档指出:Dragonfly提供了一个直接从url分配的访问器:@album.cover_image_url='http://some.url/file.jpg'但是当我在控制台中尝试时:=>#ruby-1.9.2-p290>picture.image_url="http://i.imgur.com/QQiMz.jpg"=>"http://i.imgur.com/QQ
print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上