Swift 3:如何从imageView(GIF)保存动画图像?

时间:2017-01-03 06:09:18

标签: ios swift core-image

我是swift的新手,我在一个imageView中有一些图像 动画, 现在我想将它保存在图库中,因为我们保存了简单的图像, 我怎么能这样做?

imageView.animationImages = [
            UIImage(named:"1.jpg")!,
            UIImage(named:"2.jpg")!,
            UIImage(named:"3.jpg")!,
            UIImage(named:"4.jpg")!,
            UIImage(named:"5.jpg")!
        ]

        imageView.animationDuration = 3
        imageView.startAnimating()

任何形式的帮助都将受到赞赏......(:

3 个答案:

答案 0 :(得分:3)

请参阅此处的代码 - > swift-create-a-gif-from-images-and-turn-it-into-nsdata

以上代码用于OS X,所以我稍微调整了一下。

导入ImageIOMobileCoreServices

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

    let destinationURL = name
    let destinationGIF = CGImageDestinationCreateWithURL(destinationURL as CFURL, 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 UIImage to CGImage, fitting within the specified rect
        let cgImage = img.cgImage
        // Add the frame to the GIF image
        CGImageDestinationAddImage(destinationGIF, cgImage!, properties as CFDictionary?)
    }

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

以这种方式使用它:

let documentsURL = FileManager.default.urls(for: .documentDirectory,
                                                   in: .userDomainMask).first
let path = documentsURL!.appendingPathComponent("1.gif")
createGIF(with: images, name: path, frameDelay: 0.1)
// you can check the path and open it in Finder, then you can see the file
print(path)

我还在github上做了一个演示 - > saveImagesAsGif

答案 1 :(得分:0)

使用this answer中的[UIImage]到文件的代码,您可以从UIImage数组中将这些图像另存为.gif文件,然后使用PHPhotoLibrary将文件的URL传递到PHAssetChangeRequest

func saveImages(_ images: [UIImage]) {
    let documentsURL = FileManager.default.urls(
        for: .documentDirectory,
        in: .userDomainMask
    ).first
    let imageURL = documentsURL!.appendingPathComponent("MyImage.gif")

    createGIF(with: images, name: imageURL, frameDelay: 0.5)

    PHPhotoLibrary.shared().performChanges({
        PHAssetChangeRequest.creationRequestForAssetFromImage(
            atFileURL: imageURL
        )
    })
}

/// From https://stackoverflow.com/a/42216154/5052894
private func createGIF(with images: [UIImage], name: URL, loopCount: Int = 0, frameDelay: Double) {

    let destinationURL = name
    let destinationGIF =  CGImageDestinationCreateWithURL(destinationURL as CFURL, 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 UIImage to CGImage, fitting within the specified rect
        let cgImage = img.cgImage
        // Add the frame to the GIF image
        CGImageDestinationAddImage(destinationGIF, cgImage!, properties as CFDictionary?)
    }

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

答案 2 :(得分:-1)

将您的GIF图像转换为数据,然后您可以将该数据写入Photo库:

NSData *data = <GIF Image Data>
ALAssetsLibrary *assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary writeImageDataToSavedPhotosAlbum:data metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {

    NSLog(@"Image Path", [assetURL path] );
}];

我希望这对你有用。