2017-05-09 1 views
3

내 앱에서 .gif을 사용해야하고 검색했습니다. 모두 .gif 파일을 추가하는 데 UIImage.gifImageWithName("funny") 또는 UIImage.gifWithName("jeremy")을 사용하라는 메시지가 표시됩니다. 하지만 Type 'UIImage' has no member 'gifWithName'에 오류가 발생합니다. 그 문제를 해결하는 방법 및 내 응용 프로그램에서 .gif을 사용하는 방법.iOS Swift 내 프로젝트에서 GIF 파일을 사용하는 방법

+0

당신은 라이브러리를 사용할 수 있습니다 –

+0

당신이 기본 클래스를 통합입니다 그것을 위해. https://github.com/Flipboard/FLAnimatedImage이 라이브러리는 응용 프로그램 –

+0

에서 gif를로드하는 데 도움이됩니다. iOS는 GIF 파일을 지원하지 않으므로 라이브러리를 사용해야합니다. 그래서 라이브러리가 없으면 이미지 배열을 gif 이미지로로드해야합니다. –

답변

3

다운로드있는 UIImage + Gif.swift의 https://github.com/bahlo/SwiftGif/tree/master/SwiftGifCommon에서 파일을 프로젝트에 투입 ..

// An animated UIImage 
let Gif = UIImage.gif(name: "jerry") 

// A UIImageView with async loading 
let imageView = UIImageView() 
imageView.loadGif(name: "tom")] 

demo

1

iOS에서는 .gif 개의 이미지를 직접 지원하지 않습니다. SDWebImage과 같은 타사 라이브러리의 최신 버전을 사용해야합니다. 단순화 된 솔루션은 Webview를 사용하는 것입니다.

아니면, 당신처럼 프로젝트에 추가하고 사용 GifFunctions.Swift 모든 파일 이름 예제 코드 위에 UIImageView+Extension

0
// 
// GifFunctions.swift 
// GifFunctions 
// 
// Created by Ambu Sangoli on 11/12/16. 
// Copyright © 2016 Ambu Sangoli. All rights reserved. 
// 

import UIKit 
import ImageIO 
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool { 
    switch (lhs, rhs) { 
    case let (l?, r?): 
    return l < r 
    case (nil, _?): 
    return true 
    default: 
    return false 
    } 
} 




extension UIImage { 

    public class func gifImageWithData(_ data: Data) -> UIImage? { 
     guard let source = CGImageSourceCreateWithData(data as CFData, nil) else { 
      print("image doesn't exist") 
      return nil 
     } 

     return UIImage.animatedImageWithSource(source) 
    } 

    public class func gifImageWithURL(_ gifUrl:String) -> UIImage? { 
     guard let bundleURL:URL = URL(string: gifUrl) 
      else { 
       print("image named \"\(gifUrl)\" doesn't exist") 
       return nil 
     } 
     guard let imageData = try? Data(contentsOf: bundleURL) else { 
      print("image named \"\(gifUrl)\" into NSData") 
      return nil 
     } 

     return gifImageWithData(imageData) 
    } 

    public class func gifImageWithName(_ name: String) -> UIImage? { 
     guard let bundleURL = Bundle.main 
      .url(forResource: name, withExtension: "gif") else { 
       print("SwiftGif: This image named \"\(name)\" does not exist") 
       return nil 
     } 
     guard let imageData = try? Data(contentsOf: bundleURL) else { 
      print("SwiftGif: Cannot turn image named \"\(name)\" into NSData") 
      return nil 
     } 

     return gifImageWithData(imageData) 
    } 

    class func delayForImageAtIndex(_ index: Int, source: CGImageSource!) -> Double { 
     var delay = 0.1 

     let cfProperties = CGImageSourceCopyPropertiesAtIndex(source, index, nil) 
     let gifProperties: CFDictionary = unsafeBitCast(
      CFDictionaryGetValue(cfProperties, 
       Unmanaged.passUnretained(kCGImagePropertyGIFDictionary).toOpaque()), 
      to: CFDictionary.self) 

     var delayObject: AnyObject = unsafeBitCast(
      CFDictionaryGetValue(gifProperties, 
       Unmanaged.passUnretained(kCGImagePropertyGIFUnclampedDelayTime).toOpaque()), 
      to: AnyObject.self) 
     if delayObject.doubleValue == 0 { 
      delayObject = unsafeBitCast(CFDictionaryGetValue(gifProperties, 
       Unmanaged.passUnretained(kCGImagePropertyGIFDelayTime).toOpaque()), to: AnyObject.self) 
     } 

     delay = delayObject as! Double 

     if delay < 0.1 { 
      delay = 0.1 
     } 

     return delay 
    } 

    class func gcdForPair(_ a: Int?, _ b: Int?) -> Int { 
     var a = a 
     var b = b 
     if b == nil || a == nil { 
      if b != nil { 
       return b! 
      } else if a != nil { 
       return a! 
      } else { 
       return 0 
      } 
     } 

     if a < b { 
      let c = a 
      a = b 
      b = c 
     } 

     var rest: Int 
     while true { 
      rest = a! % b! 

      if rest == 0 { 
       return b! 
      } else { 
       a = b 
       b = rest 
      } 
     } 
    } 

    class func gcdForArray(_ array: Array<Int>) -> Int { 
     if array.isEmpty { 
      return 1 
     } 

     var gcd = array[0] 

     for val in array { 
      gcd = UIImage.gcdForPair(val, gcd) 
     } 

     return gcd 
    } 

    class func animatedImageWithSource(_ source: CGImageSource) -> UIImage? { 
     let count = CGImageSourceGetCount(source) 
     var images = [CGImage]() 
     var delays = [Int]() 

     for i in 0..<count { 
      if let image = CGImageSourceCreateImageAtIndex(source, i, nil) { 
       images.append(image) 
      } 

      let delaySeconds = UIImage.delayForImageAtIndex(Int(i), 
       source: source) 
      delays.append(Int(delaySeconds * 1000.0)) // Seconds to ms 
     } 

     let duration: Int = { 
      var sum = 0 

      for val: Int in delays { 
       sum += val 
      } 

      return sum 
     }() 

     let gcd = gcdForArray(delays) 
     var frames = [UIImage]() 

     var frame: UIImage 
     var frameCount: Int 
     for i in 0..<count { 
      frame = UIImage(cgImage: images[Int(i)]) 
      frameCount = Int(delays[Int(i)]/gcd) 

      for _ in 0..<frameCount { 
       frames.append(frame) 
      } 
     } 

     let animation = UIImage.animatedImage(with: frames, 
      duration: Double(duration)/1000.0) 

     return animation 
    } 
} 

저장을 사용할 수 있습니다 또한

let gifToplay = UIImage.gifImageWithName("YourGifName") 
YourImageView.image = (image: gifToplay) 

당신이하실 수 있습니다 다른 방법을 사용하십시오.

즐길 수 있습니다 (Y)

관련 문제