ios - Creating Image Preview using Swift and Parse -
i'm writing social networking application in swift. inside app have feature user can send message timeline image attached. want able retrieve image , preview using uiimageview inside timeline. want image display next user sent image , if user did not post image image preview not appear along message. example of functionally want have way facebook , twitter display images in respective ios apps. in meantime have written following function retrieve images, i'm having trouble matching them proper user. have function posted below link download source code timeline. in advance
func loadimages()
{ var query = pfquery(classname: "imagesuploaded") query.orderbydescending("objectid") query.wherekey("user", equalto:pfuser.currentuser()) query.findobjectsinbackgroundwithblock { (objects:[anyobject]!,error:nserror!) -> void in if error == nil { let imagesobjects = objects [pfobject] object : pfobject in objects [pfobject] { let image = object["filename"] pffile image.getdatainbackgroundwithblock { (imagedata:nsdata!, error:nserror!) -> void in if error == nil { if let finalimage = uiimage(data: imagedata) { let url = image.url println(url) cell.imagedisplay.image = finalimage } } } } } } }
i did this class :
import uikit import parse class postviewcontroller: uiviewcontroller, uinavigationcontrollerdelegate, uiimagepickercontrollerdelegate { var photoselected:bool = false var activityindicator: uiactivityindicatorview = uiactivityindicatorview() @iboutlet weak var imagetopost: uiimageview! @iboutlet weak var sharetext: uitextfield! @ibaction func logout(sender: anyobject) { pfuser.logout() self.performseguewithidentifier("logout", sender: "self") } @ibaction func chooseimage(sender: anyobject) { // creation d'une variable image controlleur var image = uiimagepickercontroller() dispatch_async(dispatch_get_main_queue()){ image.delegate = self image.sourcetype = uiimagepickercontrollersourcetype.photolibrary image.allowsediting = false self.presentviewcontroller(image, animated: true, completion: nil)} } @ibaction func postimage(sender: anyobject) { var error = "" if (photoselected == false){ error = "please select image post" } else if (sharetext.text == "") { error = "please enter message" } if (error != ""){ displayalert("cannot post image", error: error) } else { activityindicator = uiactivityindicatorview(frame: cgrectmake(0, 0, 50, 50)) // on positione l'indicateur au centre de l'ecran activityindicator.center = self.view.center activityindicator.hideswhenstopped = true activityindicator.activityindicatorviewstyle = uiactivityindicatorviewstyle.gray view.addsubview(activityindicator) activityindicator.startanimating() uiapplication.sharedapplication().beginignoringinteractionevents() var post = pfobject(classname: "post") post["title"] = sharetext.text post["username"] = pfuser.currentuser().username post.saveinbackgroundwithblock{(success: bool, error: nserror!) -> void in if success == false { self.activityindicator.stopanimating() uiapplication.sharedapplication().endignoringinteractionevents() self.displayalert("could not post image", error: "please try again later") } else { let imagedata = uiimagepngrepresentation(self.imagetopost.image) let imagefile = pffile(name: "image.png", data: imagedata) post["imagefile"] = imagefile post.saveinbackgroundwithblock{(success: bool, error: nserror!) -> void in self.activityindicator.stopanimating() uiapplication.sharedapplication().endignoringinteractionevents() if success == false { self.displayalert("could not post image", error: "please try again later") } else { self.displayalert(" image posted !", error: "your image has been posted successfully") self.photoselected = false self.imagetopost.image = uiimage(named: "default-placeholder") self.sharetext.text = "" println("posted succesfully") } } } } } } func displayalert(title: string, error: string){ var alert = uialertcontroller(title: title, message: error, preferredstyle: uialertcontrollerstyle.alert) alert.addaction(uialertaction(title: "ok", style: .default, handler: {action in })) self.presentviewcontroller(alert, animated: true, completion: nil) } func imagepickercontroller(picker: uiimagepickercontroller, didfinishpickingimage image: uiimage!, editinginfo: [nsobject : anyobject]!) { println("image selected!") self.dismissviewcontrolleranimated(true, completion: nil) imagetopost.image = image photoselected = true } override func viewdidload() { super.viewdidload() photoselected = false imagetopost.image = uiimage(named: "default-placeholder") self.sharetext.text = "" } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } override func touchesbegan(touches: nsset, withevent event: uievent) { self.view.endediting(true) } func textfiledshouldreturn(textfield: uitextfield!) -> bool{ sharetext.resignfirstresponder() return true } }
edit :
you can add test :
func imagepickercontroller(picker: uiimagepickercontroller, didfinishpickingimage image: uiimage!, editinginfo: [nsobject : anyobject]!) { self.dismissviewcontrolleranimated(true, completion: nil) initialisationphoto() photoutilisateur.image = image if (photoutilisateur.image != nil){ println("image selected!") photoselected = true } } func initialisationphoto(){ //initialisation de la photo utilisateur photoselected = false photoutilisateur.image = uiimage(named: "placeholder") }
Comments
Post a Comment