Dodaj animowany obraz Gif W iPhone UIImageView
Muszę załadować animowany obraz Gif z adresu URL w UIImageview.
Kiedy użyłem normalnego kodu, obraz nie ładował się.
Czy jest jakiś inny sposób na załadowanie animowanych obrazów Gif?
12 answers
UIImageView* animatedImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
animatedImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"image1.gif"],
[UIImage imageNamed:@"image2.gif"],
[UIImage imageNamed:@"image3.gif"],
[UIImage imageNamed:@"image4.gif"], nil];
animatedImageView.animationDuration = 1.0f;
animatedImageView.animationRepeatCount = 0;
[animatedImageView startAnimating];
[self.view addSubview: animatedImageView];
Możesz załadować więcej niż jeden obraz gif.
Możesz podzielić swój gif za pomocą następującego polecenia ImageMagick :
convert +adjoin loading.gif out%d.gif
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-12-07 17:07:03
Znalazłem akceptowaną odpowiedź, ale ostatnio natknąłem się na rozszerzenie UIImage+animatedGIF UIImage. Zawiera następującą kategorię:
+[UIImage animatedImageWithAnimatedGIFURL:(NSURL *)url]
Pozwala po prostu:
#import "UIImage+animatedGIF.h"
UIImage* mygif = [UIImage animatedImageWithAnimatedGIFURL:[NSURL URLWithString:@"http://en.wikipedia.org/wiki/File:Rotating_earth_(large).gif"]];
Działa jak magia.Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-12 21:01:06
Oto najlepsze rozwiązanie do wykorzystania obrazu Gif. Dodaj SDWebImage z Github do swojego projektu.
#import "UIImage+GIF.h"
_imageViewAnimatedGif.image= [UIImage sd_animatedGIFNamed:@"thumbnail"];
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-26 06:23:50
Sprawdź ten link
I zaimportuj te klasy UIImage+animatedGIF.h, UIImage+animatedGIF.m
Użyj tego kodu
NSURL *urlZif = [[NSBundle mainBundle] URLForResource:@"dots64" withExtension:@"gif"];
NSString *path=[[NSBundle mainBundle]pathForResource:@"bar180" ofType:@"gif"];
NSURL *url=[[NSURL alloc] initFileURLWithPath:path];
imageVw.image= [UIImage animatedImageWithAnimatedGIFURL:url];
Hope this is helpfull
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-14 10:20:09
To nie spełnia wymogu korzystania z interfejsu UIImageView, ale może to uprościłoby sprawę. Czy rozważałeś użycie UIWebView?
NSString *gifUrl = @"http://gifs.com";
NSURL *url = [NSURL URLWithString: gifUrl];
[webView loadRequest: [NSURLRequest requestWithURL:url]
Jeśli chcesz, zamiast linkowania do adresu URL, który wymaga internetu, możesz zaimportować plik HTML do projektu Xcode i ustawić root w ciągu znaków.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-19 17:18:50
Oto ciekawa biblioteka: https://github.com/Flipboard/FLAnimatedImage
Przetestowałem przykład demo i działa świetnie. To dziecko UIImageView. Więc myślę, że możesz go również użyć bezpośrednio w swoim Storyboardzie.
Cheers
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-09 13:37:10
Wiem, że odpowiedź została już zatwierdzona, ale trudno nie spróbować podzielić się tym, że stworzyłem wbudowany framework, który dodaje obsługę Gif do iOS, który wygląda tak, jakbyś używał innej klasy Framework UIKit.
Oto przykład:
UIGifImage *gif = [[UIGifImage alloc] initWithData:imageData];
anUiImageView.image = gif;
Pobierz najnowszą wersję z https://github.com/ObjSal/UIGifImage/releases
-- Sal
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-15 23:21:56
Jeśli musisz załadować obraz gif z adresu URL, zawsze możesz osadzić go w znaczniku obrazu w UIWebView
.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-11 06:08:04
Możesz użyć https://github.com/Flipboard/FLAnimatedImage
#import "FLAnimatedImage.h"
NSData *dt=[NSData dataWithContentsOfFile:path];
imageView1 = [[FLAnimatedImageView alloc] init];
FLAnimatedImage *image1 = [FLAnimatedImage animatedImageWithGIFData:dt];
imageView1.animatedImage = image1;
imageView1.frame = CGRectMake(0, 5, 168, 80);
[self.view addSubview:imageView1];
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-23 12:20:00
Swift 3:
Jak sugerowano powyżej używam FLAnimatedImage z FLAnimatedImageView. I ładuję gif jako zestaw danych z xcassets. Pozwala mi to dostarczać różne gify dla iphone ' a i iPada do celów wyglądu i krojenia aplikacji. To jest o wiele bardziej wydajne niż cokolwiek innego próbowałem. Jest również łatwy do wstrzymania przy użyciu .stopanimat ().
if let asset = NSDataAsset(name: "animation") {
let gifData = asset.data
let gif = FLAnimatedImage(animatedGIFData: gifData)
imageView.animatedImage = gif
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-02 18:11:26
SWIFT 3
Oto aktualizacja dla tych, którzy potrzebują wersji Swift!.
[16]} kilka dni temu musiałem zrobić coś takiego. Ładuję dane z serwera według określonych parametrów i w międzyczasie chciałem pokazać inny obraz gif "Ładowanie". Szukałem opcji, aby to zrobić zUIImageView
, ale niestety nie znalazłem nic, aby to zrobić bez podziału .obrazy gif. Postanowiłem więc wdrożyć rozwiązanie używając UIWebView
i chcę się nią podzielić:
extension UIView{
func animateWithGIF(name: String){
let htmlString: String = "<!DOCTYPE html><html><head><title></title></head>" +
"<body style=\"background-color: transparent;\">" +
"<img src=\""+name+"\" align=\"middle\" style=\"width:100%;height:100%;\">" +
"</body>" +
"</html>"
let path: NSString = Bundle.main.bundlePath as NSString
let baseURL: URL = URL(fileURLWithPath: path as String) // to load images just specifying its name without full path
let frame = CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height)
let gifView = UIWebView(frame: frame)
gifView.isOpaque = false // The drawing system composites the view normally with other content.
gifView.backgroundColor = UIColor.clear
gifView.loadHTMLString(htmlString, baseURL: baseURL)
var s: [UIView] = self.subviews
for i in 0 ..< s.count {
if s[i].isKind(of: UIWebView.self) { s[i].removeFromSuperview() }
}
self.addSubview(gifView)
}
func animateWithGIF(url: String){
self.animateWithGIF(name: url)
}
}
Zrobiłem rozszerzenie dla UIView
, które dodaje UIWebView
jako subview i wyświetla .obrazy gif po prostu mijają jego nazwę.
Teraz w moim UIViewController
mam UIView
o nazwie "loadingView", który jest moim wskaźnikiem "loading" i kiedy chciałem pokazać .GIF image, zrobiłem coś takiego:
class ViewController: UIViewController {
@IBOutlet var loadingView: UIView!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
configureLoadingView(name: "loading.gif")
}
override func viewDidLoad() {
super.viewDidLoad()
// .... some code
// show "loading" image
showLoadingView()
}
func showLoadingView(){
loadingView.isHidden = false
}
func hideLoadingView(){
loadingView.isHidden = true
}
func configureLoadingView(name: String){
loadingView.animateWithGIF(name: "name")// change the image
}
}
Kiedy chciałem zmienić obraz gif, po prostu nazwałem funkcję configureLoadingView()
nazwą mojego nowego .obraz gif i wywołanie showLoadingView()
, hideLoadingView()
właściwie wszystko działa dobrze!.
Ale...
... jeśli obraz jest podzielony, możesz animować go w jednej linii za pomocą statycznej metody UIImage
o nazwie UIImage.animatedImageNamed
w następujący sposób:
imageView.image = UIImage.animatedImageNamed("imageName", duration: 1.0)
From the docs:
Ta metoda wczytuje serię plików przez dodanie serii liczb do podstawowej nazwy pliku podanej w parametrze name. Wszystkie obrazy zawarte w animowanym obrazie powinny mieć ten sam rozmiar i skalę.
Or you can make it z metodą UIImage.animatedImageWithImages
taką jak ta:
let images: [UIImage] = [UIImage(named: "imageName1")!,
UIImage(named: "imageName2")!,
...,
UIImage(named: "imageNameN")!]
imageView.image = UIImage.animatedImage(with: images, duration: 1.0)
From the docs:
Tworzy i zwraca animowany obraz z istniejącego zestawu obrazów. Wszystkie obrazy zawarte w animowanym obrazie powinny mieć ten sam rozmiar i skalę.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-13 19:57:46
Jeśli znasz czas trwania pliku gif, nie potrzebujesz biblioteki innej firmy.
extension UIImage {
class func getImagesFromGIF(asset: String) -> [UIImage]? {
guard let dataAsset = NSDataAsset(name: asset) else {
print("Cannot turn image named \"\(asset)\" into NSDataAsset")
return nil
}
guard let source = CGImageSourceCreateWithData(dataAsset.data as CFData, nil) else {
print("Source for the image does not exist")
return nil
}
let count = CGImageSourceGetCount(source)
var images = [UIImage]()
for i in 0..<count {
if let cgImage = CGImageSourceCreateImageAtIndex(source, i, nil) {
let image = UIImage(cgImage: cgImage)
images.append(image)
}
}
return images
}
}
Aby użyć tego rozszerzenia,
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
DispatchQueue.global().async {
if let images = UIImage.getImagesFromGIF(asset: "GIFFILENAME") {
let gifDuration = 5.0 // need to change this
DispatchQueue.main.async {
self.imageView.animationImages = images
self.imageView.animationDuration = gifDuration
self.imageView.startAnimating()
}
}
}
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
imageView.stopAnimating()
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-24 20:07:43