【问题标题】:How do I animate gif inside CollectionViewController.. (Objective-C)如何在 CollectionViewController 中为 gif 设置动画.. (Objective-C)
【发布时间】:2016-10-14 14:40:36
【问题描述】:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
              cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

    [_keyboard.collectionView registerClass:[NibCell class] forCellWithReuseIdentifier:@"Cell"];


    static NSString *identifier = @"Cell";

    NibCell *cell = [self.keyboard.collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

    UIImageView * rampageGifImage = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, cell.frame.size.width, cell.frame.size.height)];


    [rampageGifImage setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@.gif",[rampageGif objectAtIndex:indexPath.row]]]];


    [cell.layer setCornerRadius:4];

    [cell addSubview:rampageGifImage];

    return cell;
}

【问题讨论】:

  • 您不能在原生视图中使用 gif。为此使用 webView ,或者如果您真的想展示它,请使用一个打破图像并重新激活它的库。 github.com/Flipboard/FLAnimatedImage

标签: ios objective-c iphone uicollectionview gif


【解决方案1】:

您可以通过在项目中添加来自 Github 的 SDWebImage 来使用 Gif 图像。

这样试试,

#import "UIImage+GIF.h"

_imageViewAnimatedGif.image= [UIImage sd_animatedGIFNamed:@"YOURGIF_IMAGE"];

希望这会对你有所帮助。

【讨论】:

【解决方案2】:

Try it. It is small category that could loads animated GIF. Also, jpg, png, etc..

#import "ViewController.h"
#import "UIImage+Gif.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView1;
@property (weak, nonatomic) IBOutlet UIImageView *imageView2;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Usage 1 : gifWidthData
    NSData *data = [NSData dataWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"f1" withExtension:@"gif"]];

    self.imageView1.image = [UIImage gifWithData:data];

    // Usage 2 : gifWidthURL
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"f2" withExtension:@"gif"];
    self.imageView2.image = [UIImage gifWithURL:url];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-03
    • 2023-03-25
    • 2016-11-13
    • 2020-12-13
    • 1970-01-01
    • 2020-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多