UIImage+GIF.swift SDWebImage中处理GIF的分类的swift版实现
来源:互联网 发布:算法分析专业 编辑:程序博客网 时间:2024/06/10 17:08
//
// UIImage+GIF.swift
// swiftPractise
//
// Created by 云君 on 2017/6/5.
// Copyright © 2017年 pilgrim. All rights reserved.
//
import Foundation
import UIKit
import ImageIO
extension UIImage {
classfunc animatedGIFWith(data:Data?) -> UIImage? {
iflet data = data {
iflet source = CGImageSourceCreateWithData(dataas CFData,nil) {
let count = CGImageSourceGetCount(source)
var animatedImage:UIImage? = nil
if count <=1 {
animatedImage = UIImage(data: data)
} else {
var images: [UIImage] = []
var duration:TimeInterval = 0.0
for iin 0..<count {
let image = CGImageSourceCreateImageAtIndex(source, i,nil)
if image ==nil {
continue
}
duration = duration + frameDurationAt(index: i, source: source)
images.append(UIImage(cgImage: image!, scale: UIScreen.main.scale, orientation: .up))
}
if duration ==0 {
duration = (1.0 /10.0) * Double(count)
}
animatedImage = UIImage.animatedImage(with: images, duration: duration)
}
return animatedImage
} else {
returnnil
}
} else {
returnnil
}
}
classfunc frameDurationAt(index:Int, source: CGImageSource) ->TimeInterval {
var frameDuration:TimeInterval = 0.1
iflet cfFrameProperties = CGImageSourceCopyPropertiesAtIndex(source, index,nil) {
let frameProperties = cfFramePropertiesas NSDictionary
iflet gifProperties = frameProperties[kCGImagePropertyGIFDictionaryas String]as? NSDictionary {
iflet delayTimeUnclampedProp = gifProperties[kCGImagePropertyGIFUnclampedDelayTimeas String]as? NSNumber {
frameDuration = delayTimeUnclampedProp.doubleValue
} else {
iflet delayTimeProp = gifProperties[kCGImagePropertyGIFDelayTimeas String]as? NSNumber {
frameDuration = delayTimeProp.doubleValue
}
}
}
}
// Many annoying ads specify a 0 duration to make an image flash as quickly as possible.
// We follow Firefox's behavior and use a duration of 100 ms for any frames that specify
// a duration of <= 10 ms. See <rdar://problem/7689300> and <http://webkit.org/b/36082>
// for more information.
if frameDuration <0.011 {
frameDuration = 0.100
}
return frameDuration
}
classfunc animatedGIF(name: String) -> UIImage? {
let scale = UIScreen.main.scale
if scale >1.0 {
iflet retinaPath = Bundle.main.path(forResource: name.appending("@2x"), ofType:"gif") {
var data:Data?
do {
data = try Data(contentsOf: URL(fileURLWithPath: retinaPath))
} catch {
print("从path获取图片data失败")
}
iflet data = data {
iflet image = UIImage.animatedGIFWith(data: data) {
return image
}
}
}
iflet path = Bundle.main.path(forResource: name, ofType:"gif") {
var data:Data?
do {
data = try Data(contentsOf: URL(fileURLWithPath: path))
} catch {
print("从path获取图片data失败")
}
iflet data = data {
iflet image = UIImage.animatedGIFWith(data: data) {
return image
}
}
}
return UIImage(named: name)
} else {
iflet path = Bundle.main.path(forResource: name, ofType:"gif") {
var data:Data?
do {
data = try Data(contentsOf: URL(fileURLWithPath: path))
} catch {
print("从path获取图片data失败")
}
iflet data = data {
iflet image = UIImage.animatedGIFWith(data: data) {
return image
}
}
}
return UIImage(named: name)
}
}
func animatedImageByScalingAndCroppingTo(size:CGSize) -> UIImage? {
if __CGSizeEqualToSize(self.size, size) || __CGSizeEqualToSize(size, .zero) {
returnself
}
var scaledSize = size
var thumbnailPoint = CGPoint.zero
let widthFactor = size.width /self.size.width
let heightFactor = size.height /self.size.height
let scaleFactor = widthFactor > heightFactor ? widthFactor : heightFactor
scaledSize.width = self.size.width * scaleFactor
scaledSize.height = self.size.height * scaleFactor
if widthFactor > heightFactor {
thumbnailPoint.y = (size.height - scaledSize.height) *0.5
} elseif widthFactor < heightFactor {
thumbnailPoint.x = (size.width - scaledSize.width) * 0.5
}
var scaledImages: [UIImage] = []
iflet images = self.images {
for imagein images {
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
image.draw(in: CGRect(x: thumbnailPoint.x, y: thumbnailPoint.y, width: scaledSize.width, height: scaledSize.height))
iflet newImage = UIGraphicsGetImageFromCurrentImageContext() {
scaledImages.append(newImage)
}
UIGraphicsEndImageContext()
}
return UIImage.animatedImage(with: scaledImages, duration:self.duration)
} else {
returnnil
}
}
}
- UIImage+GIF.swift SDWebImage中处理GIF的分类的swift版实现
- Android中GIF的实现
- SDWebImage源码解读 之UIImage+GIF
- gif处理的工具
- 炫酷的放射状弹出按钮菜单 - Swift版 AwesomeMenu:源码+gif
- UIImage实现gif文件播放
- Gif ImageView的实现
- GIF动画的实现
- UIWebView读取Gif图 swift
- Swift gif动态图显示
- Qt 中 实现gif动画的播放
- Qt 中 实现gif动画的播放
- android中gif图实现的方案
- 'var' parameters are deprecated and will be removed in Swift 3 UIimage Gif [duplicate]
- Swift 中使用SDWebImage
- swift中字符串的处理
- 加载gif图片的方法:(需要SDWebImage方法)
- 加载gif图片的方法:(需要SDWebImage方法)
- MySQL 常用命令
- SpringBoot集成Swagger
- 用栈模拟递归解不等式
- BZOJ 1072 排列perm (状压dp)
- bzoj3441(线段树的奇怪题)
- UIImage+GIF.swift SDWebImage中处理GIF的分类的swift版实现
- MYSQL ErrorCode:24 too many opened files
- python ConnectionError 或 curl Could not resolve host
- CentOS 6.x安装配置GitLab(社区或个人Git管理)
- 解决ssmdemo项目飘红
- 不通过form表单异步提交
- ios 个人版 开发者账号申请
- Jenkins 发布后自动创建git tag
- 多版本软件并行开发