iOS 开发 加载显示gif oc代码
以下是一个简单的示例代码,用于在iOS开发中加载和显示GIF图像:
- 首先,导入
ImageIO和MobileCoreServices框架:
#import <ImageIO/ImageIO.h>
#import <MobileCoreServices/MobileCoreServices.h>
- 创建一个
UIImageView来显示GIF图像:
UIImageView *gifImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
[self.view addSubview:gifImageView];
- 加载并显示GIF图像:
NSURL *gifURL = [[NSBundle mainBundle] URLForResource:@"example" withExtension:@"gif"];
CGImageSourceRef imageSource = CGImageSourceCreateWithURL((__bridge CFURLRef)gifURL, NULL);
size_t imageCount = CGImageSourceGetCount(imageSource);
NSMutableArray *frames = [NSMutableArray array];
for (size_t i = 0; i < imageCount; i++) {
CGImageRef imageRef = CGImageSourceCreateImageAtIndex(imageSource, i, NULL);
UIImage *image = [UIImage imageWithCGImage:imageRef];
[frames addObject:image];
CGImageRelease(imageRef);
}
gifImageView.animationImages = frames;
gifImageView.animationDuration = imageCount * 0.1; // 控制播放速度
[gifImageView startAnimating];
CFRelease(imageSource);
上述代码假设你已经将GIF图像文件添加到项目中,并将其命名为example.gif。你可以将上述代码放入适当的方法中,例如viewDidLoad。
这样,你的应用程序将加载并播放名为example.gif的GIF图像
原文地址: https://www.cveoy.top/t/topic/iEGr 著作权归作者所有。请勿转载和采集!