像视频一样显示一系列图像?
Display a sequence of images like a video?
我正在 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
。还有其他想法吗?
试试这个,更改数组中的图像并编译项目,在这部分设置de时间间隔
animationImageView.animationDuration = 0.5;
http://www.appcoda.com/ios-programming-animation-uiimageview/
听起来您想要一个基于图像的动画。这是你要做的:
在你的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 是 link Apple 文档。
对于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
有一种更直接的方法,即在 UIImage
上使用 animatedImageNamed:
。看看这个:
UIImage *animatingImage = [UIImage animatedImageNamed:@"test_frame_" duration:1.0];
UIImageView *anImageView = [[UIImageView alloc] initWithImage:animatingImage];
我正在 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
。还有其他想法吗?
试试这个,更改数组中的图像并编译项目,在这部分设置de时间间隔
animationImageView.animationDuration = 0.5;
http://www.appcoda.com/ios-programming-animation-uiimageview/
听起来您想要一个基于图像的动画。这是你要做的:
在你的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 是 link Apple 文档。
对于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
有一种更直接的方法,即在 UIImage
上使用 animatedImageNamed:
。看看这个:
UIImage *animatingImage = [UIImage animatedImageNamed:@"test_frame_" duration:1.0];
UIImageView *anImageView = [[UIImageView alloc] initWithImage:animatingImage];