SWIFT - UIImage (contentsOfFile: "") МЕНЬШЕ, чем UIImage (imageLiteralResourceName: "") ПОЧЕМУ? - PullRequest
0 голосов
/ 29 января 2019

Столкнулся со странной проблемой, когда UIImage (contentsOfFile: "") МЕНЬШЕ, чем UIImage (imageLiteralResourceName: "")

В моем CALayer у меня есть ОДНА глобальная переменная

let img:UIImage? = nil

... и если viewDidLoad я загружаю img, используя " contentsOfFile ", тогда это изображение очень МЕДЛЕННО.(Например, во время TouchMove + обновить процессор до 99-100% и FPS 5-10)

img = UIImage(cgImage: UIImage(contentsOfFile: controlPath! + "/line.png")!.cgImage!, scale: 2.0, orientation: UIImage.Orientation.up)

Но ... если в viewDidLoad я загружаю тот же img, используя " imageLiteralResourceName"или UIImage (данные: NSDATA) - тогда работает отлично!Загрузка процессора низкая, FPS40-60 ПОЧЕМУ ???

img = UIImage(cgImage: UIImage(imageLiteralResourceName: "line.png").cgImage!, scale: 2.0, orientation: UIImage.Orientation.up).cgImage

Код, который рисует 100 копий этого изображения на экране:

override public func draw(in ctx: CGContext) {
...

   //here we draw this image... nothing special
   for _ in 0...99{
      ctx.draw(img.cgImage!, in: randomPositionRect)
   }

...
}

Обновление экрана:

  override public func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesMoved(touches, with: event)
    ....

    myLayer.setNeedsDisplay()
}

PS> в цели c это работает быстро в обоих случаях.Выпуск только с SWIFT

Полный код розыгрыша:

var value = self.frame.size.height/2.0  //this value is changing in touchMove

override public func draw(in ctx: CGContext) {

    let height:CGFloat = self.frame.size.height-paddingTop!-paddingBottom!

    //rotate screen
    ctx.translateBy(x: +(frame.size.width / 2), y: +(frame.size.height / 2))
    ctx.rotate(by: degreesToRadians(x: 180))
    ctx.scaleBy(x: -1.0, y: 1.0)
    ctx.translateBy(x: -(frame.size.width / 2), y: -(frame.size.height / 2))

    ////
    ctx.translateBy(x: 0.0, y: 110)

    //Define the degrees needed for each plane to create a circle
    let degForPlane = Float(360.0 / CGFloat(panelsCount!))
    let radius: CGFloat = height / 2.0


    //The current angle offset (initially it is 0... it will change through the pan function)
    let vv: CGFloat = ((160.0 / frame.size.height) * (self.value)) + 10
    var degX: CGFloat = vv
    degX -= 90
    degX += 360

    /////DRAW Carousel ////

    let rect = CGRect(x: 0, y: 0 - (originalPanelSize!.height / 2.0), width: originalPanelSize!.width, height: originalPanelSize!.height)

    for i in 0...panelsCount!-1 {

        //Create the Matrix identity
        var t: CATransform3D = CATransform3DIdentity

        //Perform rotate on the matrix identity
        t = CATransform3DRotate(t, degreesToRadians(x: degX), 1.0, 0.0, 0.0)

        //Perform translate on the current transform matrix (identity + rotate)
        t = CATransform3DTranslate(t, 0.0, 0.0, radius)

        if i > -1 && ((degX >= -180 && degX <= 90) || (degX >= 270 && degX <= 450)) {
            let affine = CGAffineTransform(a: t.m11, b: t.m12, c: t.m21, d: t.m22, tx: t.m41, ty: t.m42)

            ctx.saveGState()
            ctx.concatenate(affine)


            ctx.draw(img!, in: rect)

            ctx.restoreGState()

        }

        degX -= CGFloat(degForPlane)

    }
}

Fast Case Slow Case

1 Ответ

0 голосов
/ 29 января 2019

Мое текущее решение сейчас использовать пользовательское расширение, которое загружает изображение по NSData:

Создать файл UIImage + ext.swift

import Foundation
import UIKit

extension UIImage{

static func fromFile (path:String)->UIImage? {

    if let data = NSData(contentsOfFile: path) {
        return UIImage(cgImage: UIImage(data: data as Data)!.cgImage!, scale: UIScreen.main.scale, orientation: UIImage.Orientation.up)
    }

    return nil
}

}

Использование:

let img = UIImage.fromFile(path: "MyFoler1/MyFolder2/File.png")
...