在imageview

时间:2016-12-23 08:19:04

标签: ios uiimageview gif

我有loading.gif图片。

我的编码是: -

[cell.Vehicleimage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[DetailArray objectAtIndex:indexPath.row]valueForKey:@"resim1"]]]  placeholderImage:[UIImage imageNamed:@"loading.gif"]];

我的问题是,我想在UIImageView的placehoder上添加gif图像。但动画不起作用。

任何人都可以帮助我。

3 个答案:

答案 0 :(得分:0)

在iOS中,.gif文件不受支持。你必须去解决方法。

一种选择是分割图像并运行动画来更改图像,使其看起来像gif动画。

对于一个选项

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

您可以加载多个gif图像。

您可以使用以下ImageMagick命令拆分您的gif:

convert +adjoin loading.gif out%d.gif

第二个选项:

使用如下的第三方库:

FLAnimatedImage

第三个选项

使用WebView实现:

extension UIView{
func animateWithGIF(name 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 = NSBundle .mainBundle().bundlePath;
    let baseURL: NSURL = NSURL(fileURLWithPath: path as String); // to load images just specifying its name without full path

    let frame = CGRectMake(0, 0, self.frame.width, self.frame.height)
    let gifView = UIWebView(frame: frame)

    gifView.opaque = false // The drawing system composites the view normally with other content.
    gifView.backgroundColor = UIColor.clearColor()
    gifView.loadHTMLString(htmlString, baseURL: baseURL)

    var s: [UIView] = self.subviews 
    for i in 0..<s.count {
        if s[i].isKindOfClass(UIWebView) { s[i].removeFromSuperview() }
    }

    self.addSubview(gifView)
}

func animateWithGIF(url url: String){
    self.animateWithGIF(name: url)
}

}

答案 1 :(得分:0)

我喜欢使用CATransition,并为它设置NSTimer 这里

    CATransition  *slideTransition = [CATransition animation];
    slideTransition.duration = 1.0;
    slideTransition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    slideTransition.type = kCATransitionFade;
    slideTransition.delegate = self;
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(changeImage:)  userInfo:nil repeats:YES];

arrslidingimages是我的图像阵列

arrslidingimages=[[NSArray alloc]initWithObjects:@"01.png",@"02.png",@"03.png",@"04.png",@"05.png", nil];

-(void)changeImage:(NSTimer *)timer
{
    //change timer of the sling image and set the current page of page controller
    slideTransition.subtype =kCATransitionFromRight; // or kCATransitionFromRight
    [_Slidingimageview.layer addAnimation:slideTransition forKey:nil];
    index++;
    if(index==[arrslidingimages count]){
        index=0;
    }

    _Slidingimageview.image = [UIImage imageNamed:[arrslidingimages objectAtIndex:index]];
        // If for any reason you need to cancel the image rotation

}

只需将占位符图像设置为

即可
[UIImage imageNamed:[arrslidingimages objectAtIndex:index]];

IT会提供此类IMAGE EFFECT

希望它有所帮助。谢谢
快乐编码

答案 2 :(得分:0)

适用于swift 3&amp; 4:

如果您使用的是SDWebImage,那么最好的解决方案是使用此link中的APNGImageSerialization。将APNG图像添加到您的包中并使用它:

posterImageView.sd_setImage(with: URL(string:hdposter), placeholderImage: UIImage.animatedImageNamed("placeholderAnimated"))

它将显示动画图像,直到加载真实图像。