我尝试使用imagePickerController拾取图像后,使用Core Data从ViewController.swift中保存一个对象。保存时,我使用微调器显示视图(DynamicView)。对象将在1或2秒内保存,但是DynamicView需要7或8秒才能从superView中删除。

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]){
    dismissViewControllerAnimated(true, completion: nil)
    picture = info[UIImagePickerControllerOriginalImage] as? UIImage
    view.addSubview(DynamicView)
    var newImageData = UIImageJPEGRepresentation(picture, 1)
    objectToSave?.photo = newImageData
    progressBarDisplayer("test", true)
    dispatch_async(dispatch_get_global_queue(
        Int(QOS_CLASS_USER_INTERACTIVE.value), 0)) {
            self.save()
    }
}

func save() {
    var error : NSError?
    if(!managedObjectContext!.save(&error) ) {
        println(error?.localizedDescription)
    }else{
        println("No error, saved")
        self.DynamicView.removeFromSuperview()


    }
    NSNotificationCenter.defaultCenter().postNotificationName("load", object: nil)
}

最佳答案

从主线程调用removeFromSuperview()以确保您的UI同步:

func save() {
    var error : NSError?
    if(!managedObjectContext!.save(&error) ) {
        println(error?.localizedDescription)
    }else{
        println("No error, saved")
        dispatch_async(dispatch_get_main_queue(),{
            self.DynamicView.removeFromSuperview()
        }


    }
    NSNotificationCenter.defaultCenter().postNotificationName("load", object: nil)
}

关于ios - removeFromSuperview()花费的时间太长,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32095264/

10-08 21:26