【发布时间】:2015-01-24 22:42:18
【问题描述】:
当我在模拟器上运行我的应用程序时,它运行良好。但是,当我在我的 iPhone 上运行它时,应用程序可以正常工作,但有时它会暂停并崩溃?我该如何解决这个问题?我可以去哪里诊断问题?
我在过渡的主页的背景中加入了图像。我使用了 Instruments,在 Allocations->Category 下它使用了来自 PNG_Data 的大量内存。我只是在我的 Xcode 项目中包含了大约 12 张 .png 照片。下面是使用图像的代码。
extension Array {
func shuffled() -> [T] {
var list = self
for i in 0..<(list.count - 1) {
let j = Int(arc4random_uniform(UInt32(list.count - i))) + i
swap(&list[i], &list[j])
}
return list
}
}
import Foundation
import UIKit
class ViewController: UIViewController {
@IBAction func unwindSegue(segue: UIStoryboardSegue) {
}
@IBOutlet weak var imageView: UIImageView!
// Array of images
let images = [
UIImage(named: "nature1@2x.png")!,
UIImage(named: "nature2@2x.png")!,
UIImage(named: "desk2@2x.png")!,
UIImage(named: "new_york8@2x.png")!,
UIImage(named: "new_york9@2x.png")!,
UIImage(named: "new_york10@2x.png")!,
UIImage(named: "new_york11@2x.png")!,
UIImage(named: "new_york14@2x.png")!,
UIImage(named: "new_york15@2x.png")!,
UIImage(named: "rainy_window1@2x.png")!,
UIImage(named: "rainy_window2@2x.png")!,
UIImage(named: "new_york16@2x.png")!,
UIImage(named: "new_york17@2x.png")!,
UIImage(named: "wall_st2@2x.png")!]
// Setting durations and intervals for transitions
var index = 0
let animationDuration: NSTimeInterval = 1.5
let switchingInterval: NSTimeInterval = 4
// VIEW DID LOAD
override func viewDidLoad() {
super.viewDidLoad()
imageView.image = images[index++]
animateImageView()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
UIView.animateWithDuration(2.5, animations: {()-> Void in
self.logo.alpha = 1.0})
}
// Function that animates the background
func animateImageView() {
CATransaction.begin()
CATransaction.setAnimationDuration(animationDuration) // passing in animationDuration
CATransaction.setCompletionBlock {
let delay = dispatch_time(DISPATCH_TIME_NOW, Int64(self.switchingInterval * NSTimeInterval(NSEC_PER_SEC)))
dispatch_after(delay, dispatch_get_main_queue()) {
self.animateImageView()
}
}
let transition = CATransition()
transition.type = kCATransitionFade // the type of transition
var new_images = images.shuffled() // SHUFFLING the images array
imageView.layer.addAnimation(transition, forKey: kCATransition)
imageView.image = new_images[index]
CATransaction.commit()
index = index < images.count - 1 ? index + 1 : 0
}
【问题讨论】:
-
您必须添加错误以及它出现在代码中的位置。否则我们帮不了你。
-
在控制台中它只是说“收到内存警告”。
-
添加了上面的代码。不知道为什么图像使用这么多内存。我应该将图像文件夹放在其他地方吗?我只是将它们放在我的 Xcode 项目中。在使用图像时,我的代码效率不高吗?
-
你有所有的 i.ages 吗?有时模拟器会创建大缓存并从那里重新加载图像。
-
图片是否需要在资产目录中,或者它们可以在我的 Xcode 项目的文件夹中?
标签: ios iphone xcode swift crash