当前位置: 动力学知识库 > 问答 > 编程问答 >

swift - app crash on iphone not on simulator

问题描述:

Whenever I tap on the last row in the column, the app crashes on the iPhone, but not on the simulator. Everything else works fine. I do not get any error message. The code is working perfectly fine along with the simulator.

import UIKit

class EarthVC: UIViewController {

@IBOutlet var slideShow1: UIImageView!

override func viewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view.

slideShow1.animationImages = [

UIImage(named: "Earth1.jpg")!,

UIImage(named: "Earth2.jpg")!,

UIImage(named: "Earth3.jpg")!,

UIImage(named: "Earth4.jpg")!,

UIImage(named: "Earth5.jpg")!,

UIImage(named: "Earth6.jpg")!,

UIImage(named: "Earth7.jpg")!,

UIImage(named: "Earth8.jpg")!,

UIImage(named: "Earth9.jpg")!,

UIImage(named: "Earth10.jpg")!,

UIImage(named: "Earth11.jpg")!,

UIImage(named: "Earth12.jpg")!,

UIImage(named: "Earth13.jpg")!,

UIImage(named: "Earth14.jpg")!,

UIImage(named: "Earth15.jpg")!,

UIImage(named: "Earth16.jpg")!,

UIImage(named: "Earth17.jpg")!,

UIImage(named: "Earth18.jpg")!,

UIImage(named: "Earth19.jpg")!,

UIImage(named: "Earth20.jpg")!,

UIImage(named: "Earth21.jpg")!,

UIImage(named: "Earth22.jpg")!,

UIImage(named: "Earth23.jpg")!,

UIImage(named: "Earth24.jpg")!,

UIImage(named: "Earth25.jpg")!,

UIImage(named: "Earth26.jpg")!,

UIImage(named: "Earth27.jpg")!,

UIImage(named: "Earth28.jpg")!,

UIImage(named: "Earth29.jpg")!,

UIImage(named: "Earth30.jpg")!,

UIImage(named: "Earth31.jpg")!

]

slideShow1.animationDuration = 93

slideShow1.startAnimating()

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

/*

// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

// Get the new view controller using segue.destinationViewController.

// Pass the selected object to the new view controller.

}

*/

}

网友答案:

With all of the images you are loading, it's likely you are running out of memory on the phone - I had this problem recently. No error, but sometimes I did see "low memory warning" on the debug console.

To diagnose it, make an array of the image names, loop over that to populate an array of UIImage's, but check that they load as you go. Then you can set that as the animationImages. So,

let imageNames = [
    "Earth1.jpg",
    "Earth2.jpg",
    // ...
    "Earth31.jpg"
]

var images = [UIImage]()
for name in imageNames
{
    guard let image = UIImage(named: name) else {
        NSLog("Failed to load image \(name)")
        continue
    }
    images.append(image)
}
slideShow1.animationImages = images

You can set a breakpoint at the continue to see how far you are getting.

Assuming that is what's going on, to fix the problem, you will need to use an image editing program make the images smaller (in terms of pixels). In my case, my images were photographs taken at 4000x6000 pixels; I found that resizing them to be the size they would actually be displayed made a difference. Also, saving the files with 50% loss made them about 15% of the original size (in terms of bytes), which made them load a lot faster. I showed the original and resized/lossy images to colleagues, they couldn't see a difference (one thought it was a joke of some kind).

网友答案:

I had a similar problem with an App I made with a large number of images (100). Their existence also slowed the app down somewhat. When I reduced the file size of each image - without affecting image Quality. It worked!! Easily done in 'Preview' on Apple.

Solution:

  1. Open the images in mac OSX with 'Preview'

  2. Goto menu at top... 'Tools'...'Adjust Size'

  3. Reduce the pixel size of the image + Save

  4. Put the newly saved images back into Xcode for your App

Reducing the file sizes by 40% without seeing any reduction in image quality on App worked!

分享给朋友:
您可能感兴趣的文章:
随机阅读: