在定时器使用的UIView动画时,斯威夫特内存问题 [英] Swift Memory Issues when using UIView Animation on a Timer

查看:132
本文介绍了在定时器使用的UIView动画时,斯威夫特内存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和斯威夫特以x code 6.1编码,并试图创建带有照片之间的交叉溶解效果的简单的自动照片幻灯片。

I'm coding with Swift in Xcode 6.1 and trying to create a simple automatic photo slideshow with a cross-dissolve effect between photos.

我想最好的实施方法是创建一个计划的NSTimer一个重复,并调用一个方法,选择

I figured the best implementation would be to create a scheduled NSTimer that repeats and calls a selector method

@IBOutlet var backgroundImageView: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()

    backgroundImageView.image = UIImage(named: "1.jpg")

    let backgroundImageTimer = NSTimer.scheduledTimerWithTimeInterval(
        5.0, 
        target: self, 
        selector: "cycleBackgroundImages", 
        userInfo: nil, 
        repeats: true)
}


// Cycling through 9 different photos titled 1.jpg, 2.jpg, ..., 9.jpg
var currentPhotoID = 1
func cycleBackgroundImages() {
    if currentPhotoID < 9 { currentPhotoID++ }
    else { currentPhotoID = 1 }

    let toImage = UIImage(named: "\(currentPhotoID).jpg")
    UIView.transitionWithView(
        self.backgroundImageView, 
        duration: 2.0, 
        options: .TransitionCrossDissolve, 
        animations: { self.backgroundImageView.image = toImage }, 
        completion: nil
     })
}

当我在模拟器上每个直到所有9张图片都通过褪色,然后内存使用量在450MB左右变平的照片色彩暗淡的时间运行,程序allocs更多的内存。那用我的Mac上16GB内存模拟器中运行时没有问题。

When I run on the simulator, the program allocs more memory each time a photo is faded in until all 9 photos are faded through and then the memory usage flattens out at around 450MB. That's no problem when running using the simulator on my Mac with 16GB of memory.

但是,当我的设备(在这种情况下,iPhone 6)上运行这是一个问题。该系统给了我记忆警告,一些平滑过渡变得相当震荡,并最终应用程序崩溃。

But it is a problem when I run on the device (iPhone 6 in this case). The system gives me memory warnings, some of the smooth transitions become fairly choppy, and eventually the application crashes.

所以我想我有3个问题:

So I guess I have 3 questions:


  1. 为什么每个动画使用这么多的内存?它看起来像每个动画allocs也许50MB左右的平均和我的图像每次不超过4MB大。

  1. Why does each animation use so much memory? It looks like each animation allocs maybe around 50MB on average and my images are each no larger than 4MB.

为什么我的应用程序似乎扶住内存不再使用的图像后?不ARC自动释放他们?

Why does my app seem to hold on to the memory after the images are no longer used? Doesn't ARC free them automatically?

有什么办法解决这一问题?

Is there any way to fix this?

感谢您的帮助!

推荐答案

由于@rdelmar说,使用 imageWithContentsOfFile:

As @rdelmar said, use imageWithContentsOfFile:.

另外,我为大家提供一种替代的方式做动画,而无需使用的NSTimer(),供大家参考。

In addition, I provide you an alternative way to do the animation without using a NSTimer(), for your reference.

class ViewController: UIViewController {
        @IBOutlet var backgroundImageView: UIImageView!

        override func viewDidLoad() {
                super.viewDidLoad()

                let path = NSBundle.mainBundle().pathForResource("1", ofType: "jpg")
                backgroundImageView.image = UIImage(contentsOfFile: path!)

                cycleBackgroundImages()
        }


        // Cycling through 9 different photos titled 1.jpg, 2.jpg, ..., 9.jpg
        var currentPhotoID = 1
        func cycleBackgroundImages() {
                if currentPhotoID < 9 { currentPhotoID++ }
                else { currentPhotoID = 1 }

                CATransaction.begin()
                CATransaction.setAnimationDuration(2.0)
                CATransaction.setCompletionBlock {
                        let delay = dispatch_time(DISPATCH_TIME_NOW, Int64(5 * NSEC_PER_SEC))
                        dispatch_after(delay, dispatch_get_main_queue()) {
                                self.cycleBackgroundImages()
                        }
                }

                let transition = CATransition()
                backgroundImageView.layer.addAnimation(transition, forKey: kCATransition)
                let path = NSBundle.mainBundle().pathForResource("\(currentPhotoID)", ofType: "jpg")
                backgroundImageView.image =  UIImage(contentsOfFile: path!)

                CATransaction.commit()
        }
}

这篇关于在定时器使用的UIView动画时,斯威夫特内存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆