在我的 MapViewController 上,我能够成功地从 CloudKit 下载所有记录,并将它们作为带有(标题和副标题)的图钉放置在它们的位置。
当您单击它时,当我尝试为每个图钉创建缩略图时。我收到错误消息:
无法将“Foodie.CustomPointAnnotation”(0x7f85dff41b00)类型的值转换为“Foodie.Place”(0x1077b5fa0)。
这是 viewForAnnotation 方法:
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?
{
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(Constants.PinIdentifier)
if pinView == nil
{
pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: Constants.PinIdentifier)
pinView?.canShowCallout = true
}
pinView!.annotation = annotation
(annotation as! Place).loadMapImage { image in
if image != nil
{
UIGraphicsBeginImageContext(CGSize(width: 30, height: 30))
image.drawInRect(CGRectMake(0, 0, 30, 30))
let smallImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let imView = UIImageView(image: smallImage)
pinView?.leftCalloutAccessoryView = imView
}
}
let customPointAnnotation = annotation as! CustomPointAnnotation
pinView!.image = UIImage(named: customPointAnnotation.pinCustomImageName)
return pinView
}
这是我的模型(地点):
import UIKit
import CloudKit
import MapKit
let placeName = "name"
let placeAddress = "address"
let placeComment = "comment"
let placePhoto = "photo"
let placeRating = "rating"
let placeLocation = "location"
class Place
{
var name: String
var address: String
var comment: String?
var photo: UIImage?
var rating: Int
var location: CLLocation?
var identifier: String
var record: CKRecord!
init(record: CKRecord)
{
self.record = record
self.name = record.valueForKey(placeName) as! String
self.address = record.valueForKey(placeAddress) as! String
self.comment = record.valueForKey(placeComment) as? String
if let photoAsset = record.valueForKey(placePhoto) as? CKAsset
{
self.photo = UIImage(data: NSData(contentsOfURL: photoAsset.fileURL)!)
}
self.rating = record.valueForKey(placeRating) as! Int
self.location = record.valueForKey(placeLocation) as? CLLocation
self.identifier = record.recordID.recordName
}
// MARK: Map Annotation
var coordinate: CLLocationCoordinate2D? {
get {
return location!.coordinate
}
}
var title: String! {
get {
return name
}
}
var subtitle: String! {
get {
return address
}
}
func loadMapImage(completion: (image: UIImage!) -> ())
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0))
{
var image: UIImage!
if let mapImage = self.record.objectForKey(placePhoto) as? CKAsset
{
image = UIImage(data: NSData(contentsOfURL: mapImage.fileURL)!)
}
completion(image: image)
}
}
}
知道我做错了什么吗?提前致谢。