swift中的GIF文件

时间:2015-08-27 11:39:23

标签: ios swift

只需从系统中拖动文件,即可将.gif文件导入xcode模拟器,并自动转换为.png文件。

1 个答案:

答案 0 :(得分:0)

如果要从Xcode的模拟器相机库导入gif文件,请确保从模拟器的safari下载.gif文件或将gif从应用程序保存到相机库。 如果我们通过简单地从系统中拖动文件将.gif文件导入xcode模拟器,它将自动转换为.png文件。

    let savingPath : AnyObject = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,true)[0]

    var library:ALAssetsLibrary? = ALAssetsLibrary()
    var referenceURL : NSURL = info[UIImagePickerControllerReferenceURL] as! NSURL
    library!.assetForURL(referenceURL, resultBlock: { (asset : ALAsset!) -> Void in
        var assetRep : ALAssetRepresentation = asset.defaultRepresentation()
        let sizeOfRawDataInBytes: Int = Int(assetRep.size())
        var rawData: NSMutableData = NSMutableData(length: sizeOfRawDataInBytes)!
        var bufferPtr: UnsafeMutablePointer<Void> = rawData.mutableBytes
        var bufferPtr8: UnsafeMutablePointer<UInt8> = UnsafeMutablePointer<UInt8>(bufferPtr)

        var error: NSError? = nil
        var buffered =  assetRep.getBytes(bufferPtr8, fromOffset: 0, length: sizeOfRawDataInBytes, error: &error)
        let newData: NSData = NSData(bytesNoCopy: bufferPtr8, length: buffered, freeWhenDone: false)

        newData.writeToFile( savingPath , atomically: true)