我的代码似乎是正确的。我将图片保存到 Parse.com 上名为 profileImage 的用户列。用户图像保存正常,但我无法在UIImageView
中显示。我的代码没有错误。被困在这一段时间了。
import UIKit
import Parse
import Bolts
import ParseUI
class ProfileViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
@IBOutlet weak var previewImage: PFImageView!
@IBOutlet var finalImage: UIImageView! = UIImageView()
@IBOutlet weak var savedImage: PFImageView!
var image = [PFFile]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func chooseTapped(sender: AnyObject) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
imagePicker.mediaTypes = UIImagePickerController.availableMediaTypesForSourceType(.PhotoLibrary)!
imagePicker.allowsEditing = false;
self.presentViewController(imagePicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
self.previewImage.image = image
self.dismissViewControllerAnimated(true, completion: nil)
}
@IBAction func upload(sender: AnyObject) {
let imageToBeUploaded = self.previewImage.image
let imageData:NSData = UIImageJPEGRepresentation(imageToBeUploaded!,1.0)!
let file: PFFile = PFFile(data: imageData)!
let photoToUpload = PFObject(className: "profileImage")
//try! photoToUpload.save()
let user = PFUser.currentUser()
user!.setObject(photoToUpload, forKey: "profilePicture")
user?.saveInBackgroundWithBlock{(success: Bool! ,error: NSError?) -> Void in
if (success != nil) {
print("saving")
}
}
}
func displayImage()
{
if let userPicture = PFUser.currentUser()?["profilePicture"] as? PFFile {
userPicture.getDataInBackgroundWithBlock { (imageData: NSData?, error: NSError?) -> Void in
if (error == nil) {
self.previewImage.image = UIImage(data:imageData!)
}
}
}}
/*func postProfileImage(){
let user = PFUser.currentUser()
let profileImage:PFFile = user!["profileImage"] as! PFFile
profileImage.getDataInBackground()
let savedimage = profileImage
}*/
/*@IBAction func uploadImage(sender: AnyObject) {
let imageToBeUploaded = self.previewImage.image
let imageData = UIImagePNGRepresentation(imageToBeUploaded!)
let file: PFFile = PFFile(data: imageData!)!
let photoToUpload = PFObject(className: "profileImage")
try! photoToUpload.save()
}*/
/*
// 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.
}
*/
}