swift - 如何从本地文件创建缩略图

标签 swift xcode

我想为文件(word、excel、视频....)创建缩略图 这是我所做的:

import QuickLook

class ThumbsCreator:  NSObject {

    private var file : File?

    init(file: File?) {
        super.init()
        self.file = file
    }

    func createThumb() {
     let url = URL(string: (self.file?.path()))
 }  
}

最佳答案

经过大量搜索,我找到了这个解决方案:

import PDFKit
import AVKit
import WebKit

func createThumb() {
     let url = URL(string: (self.file?.path()))
     switch file?.type {
     case: FileType.image.rawValue:
          let image = UIImage(contentsOfFile: (url?.path)!)
          _finalImage = self.createScaledImage(image: image!)
          break 
     case: FileType.office.rawValue:  
          //Loading.......
          break
     case FileType.Pdf.rawValue:
          guard let doc = PDFDocument(url: url!) else {return}
          guard let page = doc.page(at: 0) else {return}
          _finalImage = page.thumbnail(of: CGSize(width: 768, height: 1024), for: .cropBox)
          break
     case: FileType.video.rawValue:
          let asset = AVAsset(url: url!)
          let imageGenerator = AVAssetImageGenerator(asset: asset)
          imageGenerator.appliesPreferredTrackTransform = true
          let time = CMTime(seconds: 2, preferredTimescale: 1)
          do {
          let imageRef = try imageGenerator.copyCGImage(at: time, actualTime: nil)
          _finalImage = UIImage(cgImage: imageRef)
          } catch let error{
            print("Error: \(error)")
          }
         break

}
 } 

func createScaledImage(image: UIImage) {

    let THUMB_WIDTH = 150.0 - 40.0
    let THUMB_HEIGHT = THUMB_WIDTH - 23.0
    var itemThumb = resizeImage(image: image, constraintSize: CGSize(width: THUMB_WIDTH, height: THUMB_HEIGHT))
    let thumbRect = CGRect(x: 0, y: 0, width: 10, height: 10)
    UIGraphicsBeginImageContextWithOptions(thumbRect.size, true, 0.0)
    let context = UIGraphicsGetCurrentContext()

    // Fill a white rect
    context?.setFillColor(gray: 1.0, alpha: 1.0)
    context?.fill(thumbRect)

    // Stroke a gray rect
    let comps : [CGFloat] = [0.8, 0.8, 0.8, 1]
    let colorSpace = CGColorSpaceCreateDeviceRGB()
    let strokeColor = CGColor(colorSpace: colorSpace, components: comps)
    context?.setStrokeColor(strokeColor!)
    UIRectFrame(thumbRect)
    //CGColorRelease(strokeColor!)

    itemThumb.draw(in: thumbRect.insetBy(dx: 1, dy: 1))

    itemThumb = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()
    self.finishThumCreation(image: image)
} 
}

关于swift - 如何从本地文件创建缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56149503/

相关文章:

ios - Crashlytics 不显示崩溃

swift - 如何导出使用 SecKeyGeneratePair 生成的公钥 <SecKey> 以在服务器上使用?

ios - CoreData FetchRequest 问题

ios - FirebaseApp.configuration() 未解析的标识符

ios - UIActivityTypePostToFacebook 如何发布图像和文本 xcode ios 文本的 NSDictionary 键

iOS 8.1图像显示

ios - 如果我在 XIB 中设计了 textField,如何以编程方式更改 UItextField 的 placeHolder 文本

ios - 使用预处理器宏,如何更改开发、暂存和生产环境的文件名?

iphone - 如何删除 iOS 应用附带的敏感信息

json - 使用 Alamofire 进行 GET 请求调用后没有响应