2015-10-26 131 views
1

我想爲我的註釋視圖和從解析下載的圖像製作左側標註附件。出於某種原因,與圖像相關的圖像沒有顯示出來,而左側的標註配件是空白的。縮略圖圖像不顯示在MKMapView註釋視圖

這裏是我當前的代碼:

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 

    if annotation is ImageAnnotation { 

     let reuseId = "image" 

     var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) 
     if anView == nil { 
      anView = MKAnnotationView(annotation: ImageAnnotation() as MKAnnotation, reuseIdentifier: reuseId) 
      anView!.canShowCallout = true 
     } else { 

      anView!.annotation = ImageAnnotation() as MKAnnotation 
     } 

     let cpa = annotation as! ImageAnnotation 
     anView!.image = UIImage(named: cpa.imageNameI) 

     let button = UIButton(type: UIButtonType.DetailDisclosure) // button with info sign in it 

     anView!.rightCalloutAccessoryView = button 

     anView!.leftCalloutAccessoryView = UIImageView(frame: CGRectMake(0, 0, 59, 59)) 


     return anView 
    } 

    return nil 
} 



func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) { 

     if let Annotation = view.annotation as? ImageAnnotation { 

      if let thumbnailImageView = view.leftCalloutAccessoryView as? UIImageView { 

func loadImage() { 

    let Coordinate = view.annotation?.coordinate 

    let lat = Coordinate?.latitude 
    let lon = Coordinate?.longitude 

    let AnCoordinate = PFGeoPoint(latitude: lat!, longitude: lon!) 

    let query = PFQuery(className: "ImageAn") 
    query.whereKey("shoutoutlocation", equalTo: AnCoordinate) 
    query.findObjectsInBackgroundWithBlock ({(objects:[AnyObject]?, error: NSError?) in 
     if(error == nil){ 

      _ = objects as! [PFObject] 

      for object in objects! { 

       let thumbNail = object["image"] as! PFFile 

       thumbNail.getDataInBackgroundWithBlock({ 
        (imageData: NSData?, error: NSError?) -> Void in 
        if (error == nil) { 

         let Image = UIImage(data:imageData!) 

         thumbnailImageView.image = Image 

         } 
        }) 
       } 

      }else{ 
       print("Error in retrieving \(error)") 
      } 
     }) 

     } 
    } 

    } 
} 

誰能告訴我什麼,我做錯了什麼? 謝謝

回答

1

您需要在分配圖像數據後刷新完成塊中的視圖,或者可以使用PFImageView而不是UIImageView

PFImageView包含在ParseUI框架中,並在文件正在下載時自動處理顯示佔位符圖像,然後在準備好時更新視圖。

典型用法

// Set placeholder image 
thumbnailImageView.image = UIImage(named: "thumbnail_placeholder") 

// Set remote image (PFFile) 
thumbnailImageView.file = object["image"] as! PFFile 

// Once the download completes, the remote image will be displayed 
thumbnailImageView.loadInBackground { (image: UIImage?, error: NSError?) -> Void in 
    if (error != nil) { 
     // Log details of the failure 
     println("Error: \(error!) \(error!.userInfo!)") 
    } else { 
     // profile picture loaded 
    } 
} 
+0

請問佔位符圖像名稱僅僅是類的名字? –

+0

林有點困惑。 –

+0

佔位符圖像名稱是您決定添加的任何內容。您應該爲應用的圖片資源添加某種默認圖片,然後在沒有網絡連接從Parse下載數據時顯示該圖片資源。例如,你可以有一個像問號一樣簡單的東西https://www.adagio.com/images5/question_mark.jpg – Russell