我在iOS上尝试图像处理算法。我有一个包含图像test_frame1
... test_frame100
(等等)的资产目录,我会对其进行处理。
除此之外,我需要按顺序显示图像,就好像它们是视频帧一样。
我想到的第一件事是在延迟几毫秒之后依次更改UIImage
的图像。我正在异步这样做。这是代码:
let imageShowQueue = dispatch_queue_create("com.grigoryptashko.ImageShowQueue",
DISPATCH_QUEUE_SERIAL)
let frameShowDelay = Int64(0.2 * Double(NSEC_PER_SEC)) // show images with 200 ms delay, but it doesn't actually work this way
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) {
var dTime = dispatch_time(DISPATCH_TIME_NOW, frameShowDelay)
for i in 1...1000 {
dispatch_after(dTime, self.imageShowQueue) {
dispatch_async(dispatch_get_main_queue()) {
NSLog("test_frame\((i % 100) + 1)")
self.imgView.image = UIImage(named: "test_frame\((i % 100) + 1)")
self.imgView.setNeedsDisplay()
}
}
dTime = dispatch_time(dTime, frameShowDelay)
}
}
它实际上有效,但FPS很快变得太低,比如1 FPS甚至更慢。还有其他方法可以达到这个目的吗?
也许,有一些我不知道的方式。例如,最近我了解了Accelerate
框架和Apple Metal
。蚂蚁其他想法?
答案 0 :(得分:2)
听起来你想要一个基于图像的动画。这是你要做的:
在viewDidLoad
self.<name of the UIImageView you want to animate>.animationImages = [[NSArray alloc] initWithObjects: [UIImage imageNamed:@"Image Name"],
[UIImage imageNamed:@"Image Name"],
[UIImage imageNamed:@"Image Name"],
[UIImage imageNamed:@"Image Name"],
[UIImage imageNamed:@"Image Name"],
[UIImage imageNamed:@"Image Name"],
[UIImage imageNamed:@"Image Name"],
[UIImage imageNamed:@"Image Name"],nil];
/* Do this until you have all the images in the array */
self.<name of the UIImageView you want to animate>.animationDuration = 1.0f;
self.<name of the UIImageView you want to animate>.animationRepeatCount = 1;
如果您想要动画启动,请使用[<name of the UIImageView you want to animate> startAnimating];
。 animationDuration
和animationRepeatCount
的数字可以更改为您希望它们的值。 Here是Apple Docs的链接。
对于Swift开发:
<name of the UIImageView you want to animate>.animationImages = [
UIImage(named: "Image Name")!,
UIImage(named: "Image Name")!,
UIImage(named: "Image Name")!,
UIImage(named: "Image Name")!,
UIImage(named: "Image Name")!,
UIImage(named: "Image Name")!,
UIImage(named: "Image Name")!,
UIImage(named: "Image Name")!
]
/* Do this until you have all the images in the array */
<name of the UIImageView you want to animate>.animationDuration = 1.0
<name of the UIImageView you want to animate>.animationRepeatCount = 1
答案 1 :(得分:1)
通过在animatedImageNamed:
上使用UIImage
,可以更直接地实现此目的。看看这个:
UIImage *animatingImage = [UIImage animatedImageNamed:@"test_frame_" duration:1.0];
UIImageView *anImageView = [[UIImageView alloc] initWithImage:animatingImage];
答案 2 :(得分:0)
尝试这个,更改数组中的图像并编译项目,在此部分设置时间间隔
animationImageView.animationDuration = 0.5;
http://www.appcoda.com/ios-programming-animation-uiimageview/