내 앱에서 .gif
을 사용해야하고 검색했습니다. 모두 .gif
파일을 추가하는 데 UIImage.gifImageWithName("funny")
또는 UIImage.gifWithName("jeremy")
을 사용하라는 메시지가 표시됩니다. 하지만 Type 'UIImage' has no member 'gifWithName'
에 오류가 발생합니다. 그 문제를 해결하는 방법 및 내 응용 프로그램에서 .gif
을 사용하는 방법.iOS Swift 내 프로젝트에서 GIF 파일을 사용하는 방법
3
A
답변
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")]
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)
관련 문제
- 1. iOS : GitHub 프로젝트에서 .framework 파일을 생성하는 방법
- 2. 내 프로젝트에서 docx4j를 사용하는 방법?
- 3. 내 프로젝트에서 EntityX를 사용하는 방법?
- 4. 내 프로젝트에서 SOS.DLL을 사용하는 방법
- 5. 내 프로젝트에서 cmockery를 사용하는 방법
- 6. Objective-C 프로젝트에서 Swift Pod 프레임 워크를 가져오고 사용하는 방법
- 7. 프로젝트에서 jpg로 gif 사용
- 8. 내 Java 프로젝트에서 Neo4j를 사용하는 방법
- 9. 존재 ios 프로젝트에서 cocoapods를 사용하는 방법
- 10. 내 HTML 프로젝트에서 sass/scss 파일을 사용하는 방법?
- 11. 내 프로젝트에서 크로스 도메인 xml 파일을 사용하는 방법
- 12. 내 vs2010 프로젝트에서 wxformbuilder genetated C++ 파일을 사용하는 방법
- 13. iPhone 프로젝트에서 DCMTK를 사용하는 방법
- 14. 플렉스 프로젝트에서 .swc 파일을 사용하는 방법
- 15. Eclipse 프로젝트에서 외부 클래스 파일을 사용하는 방법
- 16. devos prod의 ios swift 프로젝트에서 두 개의 다른 GoogleService-info.plist 파일을 사용하는 방법은 무엇인가요?
- 17. 테스트 프로젝트에서 app.config 파일을 사용하는 방법
- 18. Visual Basic 프로젝트에서 .c 파일을 사용하는 방법?
- 19. 컬렉션보기에서 GIF 처리 안 함 Swift
- 20. Swift 프로젝트에서 FLEX 사용
- 21. use_frameworks 플래그를 사용하지 않고 swift 프로젝트에서 objective c 파일을 사용합니다.
- 22. 설치 프로젝트에서 사용하는 방법
- 23. Eclipse 프로젝트에서 Netbeans Jar 파일을 사용하는 방법
- 24. Google TV 프로젝트에서 .so 파일을 사용하는 방법
- 25. Visual C# 프로젝트에서 .ini 파일을 사용하는 방법
- 26. 스칼라 프로젝트에서 .C 파일을 사용하는 방법
- 27. cocos2d에서 .gif 파일을 실행하는 방법
- 28. 내 iOS 프로젝트에서 pjsip을 어떻게 사용합니까?
- 29. 내 모노 프로젝트에서 JdSoft.Apple.Apns.Notifications를 사용하는 방법
- 30. 내 프로젝트에서 netbeans 모듈을 사용하는 방법?
당신은 라이브러리를 사용할 수 있습니다 –
당신이 기본 클래스를 통합입니다 그것을 위해. https://github.com/Flipboard/FLAnimatedImage이 라이브러리는 응용 프로그램 –
에서 gif를로드하는 데 도움이됩니다. iOS는 GIF 파일을 지원하지 않으므로 라이브러리를 사용해야합니다. 그래서 라이브러리가 없으면 이미지 배열을 gif 이미지로로드해야합니다. –