UIImage无法保存到磁盘

时间:2015-09-28 16:48:19

标签: ios swift uiimage uipickerview archive

一切正常,但我添加了一些代码将图像保存到磁盘,一旦拍摄它似乎不起作用,不给我一个错误,任何想法?

func importPicture(action: UIAlertAction! = nil) {

    let picker = UIImagePickerController()
    picker.allowsEditing = true
    picker.sourceType = .Camera
    picker.delegate = self
    presentViewController(picker, animated: true, completion: nil)
}


func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

    let _ = info[UIImagePickerControllerMediaType] as! NSString
    var newImage: UIImage

    if let possibleImage = info["UIImagePickerControllerEditedImage"] as? UIImage {
        newImage = possibleImage
    } else if let possibleImage = info["UIImagePickerControllerOriginalImage"] as? UIImage {
        newImage = possibleImage
    } else {
        return
    }

    let imageName = NSUUID().UUIDString
    let imagePath =          getDocumentsDirectory().stringByAppendingPathComponent(imageName)

    if let jpegData = UIImageJPEGRepresentation(newImage, 80) {
        jpegData.writeToFile(imagePath, atomically: true)

    }

    dismissViewControllerAnimated(true, completion: nil)

    images.insert(newImage, atIndex: 0)
    collectionView.reloadData()

    if mcSession.connectedPeers.count > 0 {
        let imageData = UIImagePNGRepresentation(newImage)
        var error: NSError?
        do {
            try mcSession.sendData(imageData!, toPeers: mcSession.connectedPeers, withMode: .Reliable)
        }
        catch let error1 as NSError {
            error = error1
        }

        if error != nil {
            let ac = UIAlertController(title: "Send error", message:     error!.localizedDescription, preferredStyle: .Alert)
            ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
            presentViewController(ac, animated: true, completion: nil)
        }
    }
}

func getDocumentsDirectory() -> NSString {


    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,   .UserDomainMask, true)
    let documentsDirectory = paths[0]
    return documentsDirectory
}

1 个答案:

答案 0 :(得分:0)

$statuses = array('SHIPPED', 'FAILED', 'COMPLETED');
$output = array_fill_keys($statuses, 0);

foreach ($response as $feedback) {
    foreach($statuses as $status){
        if($feeedback->status === $status){
            $output[$status]++;
        }
    }

}

使用此代替,测试并正常工作