QLThumbnailGenerator 不生成缩略图
QLThumbnailGenerator doesn't generate the thumbnail
我尝试在 Simulator/Device 中为 iOS 和 iPadOS 使用 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
}
})
})
}
然后我尝试将图像放入包中。
获取文件 url 使用:
Bundle.main.url(forResource: "flowers", withExtension: "png")
它神奇地起作用了! ...但没有 fileURLWithPath
方法。
但是,访问通过 iTunes 上传到应用程序的文档目录中的相同文件时,出现读取错误。
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."}}
唯一一张图片a get i TYPE = 0,白色空的。
而且,和以前一样,在模拟器上没有什么好....
Error Domain=NSPOSIXErrorDomain Code=22 "couldn't issue sandbox extension com.apple.app-sandbox.read for...
我错过了一些使其正常工作的测试?
已在 iOS13.2 测试版上解决。
在设备上正常,仍然在模拟器上阅读问题
我遇到了同样的问题。我通过将 representationTypes 从 .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
}
}
}
这对我有用,我创建了这个方法:
import QuickLook
...
func generateThumbnailFromQuickLook(url: URL,
type: QLThumbnailGenerator.Request.RepresentationTypes,
completionHandler: @escaping (Bool, UIImage?) -> Void) {
let size = CGSize(width: 300, height: 250)
let scale = UIScreen.main.scale
let request = QLThumbnailGenerator.Request(fileAt: url,
size: size,
scale: scale,
representationTypes: type)
let generator = QLThumbnailGenerator.shared
generator.generateBestRepresentation(for: request) { thumbnail, error in
if let error = error {
print(error)
}
completionHandler(thumbnail != nil, thumbnail?.uiImage)
}
}
然后我检查表示类型 thumbnail
是否失败。如果失败,我尝试获取表示类型 all
:
let url: URL = <url file>
generateThumbnailFromQuickLook(url: url, type: .thumbnail) { success, image in
if success == false {
generateThumbnailFromQuickLook(url: url, type: .all) { success, image in
// use the thumbnail here
}
} else if let image = image {
// use the thumbnail here
}
}
我尝试在 Simulator/Device 中为 iOS 和 iPadOS 使用 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
}
})
})
}
然后我尝试将图像放入包中。
获取文件 url 使用:
Bundle.main.url(forResource: "flowers", withExtension: "png")
它神奇地起作用了! ...但没有 fileURLWithPath
方法。
但是,访问通过 iTunes 上传到应用程序的文档目录中的相同文件时,出现读取错误。
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."}}
唯一一张图片a get i TYPE = 0,白色空的。
而且,和以前一样,在模拟器上没有什么好....
Error Domain=NSPOSIXErrorDomain Code=22 "couldn't issue sandbox extension com.apple.app-sandbox.read for...
我错过了一些使其正常工作的测试?
已在 iOS13.2 测试版上解决。 在设备上正常,仍然在模拟器上阅读问题
我遇到了同样的问题。我通过将 representationTypes 从 .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
}
}
}
这对我有用,我创建了这个方法:
import QuickLook
...
func generateThumbnailFromQuickLook(url: URL,
type: QLThumbnailGenerator.Request.RepresentationTypes,
completionHandler: @escaping (Bool, UIImage?) -> Void) {
let size = CGSize(width: 300, height: 250)
let scale = UIScreen.main.scale
let request = QLThumbnailGenerator.Request(fileAt: url,
size: size,
scale: scale,
representationTypes: type)
let generator = QLThumbnailGenerator.shared
generator.generateBestRepresentation(for: request) { thumbnail, error in
if let error = error {
print(error)
}
completionHandler(thumbnail != nil, thumbnail?.uiImage)
}
}
然后我检查表示类型 thumbnail
是否失败。如果失败,我尝试获取表示类型 all
:
let url: URL = <url file>
generateThumbnailFromQuickLook(url: url, type: .thumbnail) { success, image in
if success == false {
generateThumbnailFromQuickLook(url: url, type: .all) { success, image in
// use the thumbnail here
}
} else if let image = image {
// use the thumbnail here
}
}