Swift:内存压力事件2 vm 0

时间:2019-05-21 13:30:37

标签: swift xcode

我在Xcode控制台中看到一条消息,Received memory pressure event 1 vm pressure 0。我不太确定是什么原因造成的,为什么,但是当我在相机的视图控制器中时,我确实知道此打印。我猜这没什么可担心的,但是它指向我应该尽快解决的问题吗?

FirstViewController: UIViewController {

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        let image = info[UIImagePickerController.InfoKey.editedImage] as! UIImage
        let croppedImage = cropToBounds(image: image, width: 10, height: 10)
        self.productImage.image = croppedImage
        print("size: \(croppedImage.size)")
        print("original size: \(image.size)")
        self.dismiss(animated: true, completion: nil)
    }

    func setupCameraPicker() {
        if UIImagePickerController.isSourceTypeAvailable(.camera) {
            let imagePicker = UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.sourceType = .camera;
            imagePicker.allowsEditing = true
            self.present(imagePicker, animated: true, completion: nil)
        }
    }
}

1 个答案:

答案 0 :(得分:1)

自动释放池

autoreleasepool { 
   let image = info[UIImagePickerController.InfoKey.editedImage] as! UIImage
    let croppedImage = cropToBounds(image: image, width: 10, height: 10)
    self.productImage.image = croppedImage
    print("size: \(croppedImage.size)")
    print("original size: \(image.size)")
    self.dismiss(animated: true, completion: nil)
 }

尝试将您的代码放入autoreleasepool Foundation的NSAutoreleasePool类型(后来抽象为@autoreleasepool块)是iOS开发中的一个非常老的概念。在iOS的Obj-C时代,这种类型的使用对于防止在特定情况下应用程序的内存耗尽很重要。随着ARC和Swift的出现和发展,很少有人仍然需要手动操作内存,这使它成为一种罕见的情况。