2017-01-03 2 views
2

: Ich bin zu schnell und ich habe einige Bilder dass Imageview in einem animierten, jetzt will ich es in der Galerie speichern, wie wir einfache Bild speichern, Wie kann ich das tun?Swift 3: Wie man animierte Bilder von imageView (GIF) speichert?

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

Jede Art von Hilfe wird ... angegeben sind (:

+2

Mögliche Duplikat von [ein Bild zu Fotos Speichern Bibliothek mit Swift 2.0] (http://stackoverflow.com/questions/35761247/saving-an-image-to-photos-library-using-swift-2-0) –

+1

@A rtemNovichkov Ich möchte Bild als GIF speichern, dass animieren. bitte entfernen Sie das mögliche doppelte Flag davon –

Antwort

-1

in Daten Konvertieren Sie Ihre GIF-Bild und dann können Sie diese Daten zu Photothek schreiben:

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

ich diese Hoffnung könnte Ihnen nützlich

+0

wie man BILDER in Daten umwandelt? Ich habe 5 Bild –

+0

Ich denke, Konvertieren von Bildern in Daten wird eine schwierige Aufgabe sein. –

+0

geben Sie weitere Informationen. –

2

den Code finden Sie hier. ->swift-create-a-gif-from-images-and-turn-it-into-nsdata

Der obige Code wird für OS X verwendet, also habe ich ihn ein wenig angepasst.

Import ImageIO und MobileCoreServices

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

es auf diese Weise verwenden:

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) 

Ich habe auch eine Demo auf Github ->saveImagesAsGif