我尝试在iOS和iPadOS的Simulator / Device中使用QLThumbnailGenerator,但是它不起作用。 我只能从文档目录的文件中获取标准的空白缩略图,而无法获取丰富图标。
在沙箱中处理文件有些进展,但没什么用。
您可以使用它吗? 可能获得许可的地方有问题...但是什么?从我的应用程序中,我可以列出文件,并读取(打开)它们。
@IBAction func generateDidSelect(_ sender: Any) {
let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last
print("DOC: \(docDir)")
let absPath = URL(fileURLWithPath: docDir ?? "").appendingPathComponent("flowers.png").absoluteString
//[[NSBundle mainBundle]URLForResource:@"flowers" withExtension:@"png"];
let fileURL = URL(fileURLWithPath: absPath)
fileURL.startAccessingSecurityScopedResource()
let size = CGSize(width: 200, height: 200)
let scale = UIScreen.main.scale
let isFile = fileURL.isFile()
let exists = FileManager.default.fileExists(atPath: fileURL.path)
print("isFILE? \(isFile ? "YES" : "NO") exists? \(exists ? "YES" : "NO") \nFILE: \(fileURL)")
let request = QLThumbnailGenerationRequest(fileAtURL: fileURL, size: size, scale: scale, representationTypes: QLThumbnailGenerationRequestRepresentationTypeAll)
//request.iconMode = YES;
QLThumbnailGenerator.shared().generateRepresentations(for: request, updateHandler: { thumbnail, type, error i
DispatchQueue.main.async(execute: {
print(String(format: "*** TYPE: %ld ****", Int(type)))
let uiimage = thumbnail?.uiImage
let cgImage = thumbnail?.cgImage
if let uiimage = uiimage {
print("uiimage: \(uiimage)")
}
if let cgImage = cgImage {
print("cgImage: \(cgImage)")
}
if uiimage != nil {
self.thumbnailImageView.image = uiimage
}
if error != nil {
if let error = error {
print("ERROR: \(error)")
}
//self.thumbnailImageView.image = [UIImage imageWithContentsOfFile:fileURL.path]; // test read, works
}
})
})
}
然后我尝试将图像放入捆绑包中。
通过以下方式获取文件网址:
Bundle.main.url(forResource: "flowers", withExtension: "png")
它神奇地起作用了! ...但是没有fileURLWithPath
方法。
但是,访问通过iTunes上传到应用程序的Documents目录中的相同文件,我遇到读取错误。
2019-10-01 12:41:27.167091+0200 test_thumb_obj[618:57118] DOC: /var/mobile/Containers/Data/Application/BE4A5950-5D24-4620-A1FE-B837222E8B64/Documents
2019-10-01 12:41:27.196739+0200 test_thumb_obj[618:57118] isFILE? YES exists? YES
FILE: file:///var/mobile/Containers/Data/Application/BE4A5950-5D24-4620-A1FE-B837222E8B64/Documents/flowers.png
2019-10-01 12:41:27.233546+0200 test_thumb_obj[618:57118] *** TYPE: 0 ****
2019-10-01 12:41:27.233788+0200 test_thumb_obj[618:57118] uiimage:
2019-10-01 12:41:27.233858+0200 test_thumb_obj[618:57118] cgImage: (DP)
< (kCGColorSpaceDeviceRGB)>
width = 493, height = 640, bpc = 8, bpp = 32, row bytes = 1984
kCGImageAlphaPremultipliedFirst | kCGImageByteOrder32Little | kCGImagePixelFormatPacked
is mask? No, has masking color? No, has soft mask? No, has matte? No, should interpolate? Yes
2019-10-01 12:41:27.234761+0200 test_thumb_obj[618:57118] *** TYPE: 1 ****
2019-10-01 12:41:27.234836+0200 test_thumb_obj[618:57118] uiimage: (null)
2019-10-01 12:41:27.234865+0200 test_thumb_obj[618:57118] cgImage: (null)
2019-10-01 12:41:27.234943+0200 test_thumb_obj[618:57118] ERROR: Error Domain=QLThumbnailErrorDomain Code=2 "No cached thumbnail"
2019-10-01 12:41:27.262228+0200 test_thumb_obj[618:57118] *** TYPE: 2 ****
2019-10-01 12:41:27.262317+0200 test_thumb_obj[618:57118] uiimage: (null)
2019-10-01 12:41:27.262349+0200 test_thumb_obj[618:57118] cgImage: (null)
2019-10-01 12:41:27.262452+0200 test_thumb_obj[618:57118] ERROR: Error Domain=QLThumbnailErrorDomain Code=0 "Could not generate a thum
bnail" UserInfo={NSUnderlyingError=0x281676940 {Error Domain=NSCocoaErrorDomain Code=256 "The file couldn’t be opened."}}
唯一获得i TYPE = 0的图像,白色为空。
而且,像以前一样,在模拟器上也没有好处。...
Error Domain=NSPOSIXErrorDomain Code=22 "couldn't issue sandbox extension com.apple.app-sandbox.read for...
我错过了一些测试才能使其正常工作吗?
答案 0 :(得分:0)
已在iOS13.2 beta中解决。 在设备正常的情况下,仍然在模拟器上阅读问题
答案 1 :(得分:0)
我遇到了同样的问题。我通过将表示类型从 .all 更改为 .thumbnail 来解决它
let request = QLThumbnailGenerator
.Request(fileAt: fileURL, size: size, scale: scale,
representationTypes: .thumbnail)
或者如果你愿意,你可以尝试创建一个递归。在 generateRepresentations
调用 20-30 次后,错误“无缓存缩略图”停止出现并且图像可用。
func thumbnail(for fileURL: URL, size: CGSize, scale: CGFloat) {
QLThumbnailGenerator.shared.generateRepresentations(for: request) { (thumbnail, type, error) in
let uiimage = thumbnail?.uiImage
if uiimage == nil {
thumbnail(fileURL, size, scale)
}
else {
//image is available
}
}
}