ios – Swift – 从图像创建GIF并将其转换为NSData

前端之家收集整理的这篇文章主要介绍了ios – Swift – 从图像创建GIF并将其转换为NSData前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这可能是一个业余问题,但是虽然我已经可扩展地搜索了Stack Overflow,但我还是无法得到我特定问题的答案.

通过遵循Github示例,我成功地从一组图像创建了一个GIF文件

func createGIF(with images: [NSImage],name: NSURL,loopCount: Int = 0,frameDelay: Double) {

    let destinationURL = name
    let destinationGIF = CGImageDestinationCreateWithURL(destinationURL,kUTTypeGIF,images.count,nil)!

    // This dictionary controls the delay between frames
    // If you don't specify this,CGImage will apply a default delay
    let properties = [
        (kCGImagePropertyGIFDictionary as String): [(kCGImagePropertyGIFDelayTime as String): frameDelay]
    ]


    for img in images {
        // Convert an NSImage to CGImage,fitting within the specified rect
        let cgImage = img.CGImageForProposedRect(nil,context: nil,hints: nil)!

        // Add the frame to the GIF image
        CGImageDestinationAddImage(destinationGIF,cgImage,properties)
    }

    // Write the GIF file to disk
    CGImageDestinationFinalize(destinationGIF)
}

现在,我想将实际的GIF转换为NSData,以便将其上传到Firebase,并能够在其他设备上检索它.

为了实现我的目标,我有两个选择:要么找到如何使用上面的代码提取创建的GIF(这似乎是在创建文件时直接创建),要么使用函数参数上的图像来创建一个新的GIF,但保持NSData格式.

有没有人对如何做到这一点有任何想法?

解决方法

由于没有人继续超过六个月,我将在这里回答@Sachin Vas的评论

您可以使用NSData获取数据(contentsOf:URL)

猜你在找的iOS相关文章