2017-01-03 17 views
2

私は迅速に新しいですし、私はは1つのImageViewの、 でをアニメ化することを、いくつかの画像を持っている今、私たちは、単純な画像を保存すると、ギャラリーに保存したい、 はどのように私はそれを行うことができますか?Swift 3:imageView(GIF)からアニメーション画像を保存するには?

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() 

ヘルプの任意の種類は、(...理解されるであろう。

+2

が重複する可能性をライブラリを使用してSwift 2.0](http://stackoverflow.com/questions/35761247/saving-an-image-to-photos-library-using-swift-2-0) –

+1

@A rtemNovichkovアニメーションのGIF形式で画像を保存したい。その可能性のある重複フラグを削除してください –

答えて

-1

は、あなたのGIF画像をデータに変換し、その後、フォトライブラリにそのデータを書き込むことができます。

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]); 
}]; 

私はこのことを願っていますあなたのために有用である可能性

+0

IMAGESをどのようにデータに変換するのですか?私は5つの画像を持っています –

+0

私はデータに画像を変換することは難しい作業になると思います。 –

+0

いくつかの情報を追加します。 –

2

がここにコードを参照してください - 。>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

関連する問題