【发布时间】:2014-01-02 01:56:23
【问题描述】:
我将MKPointAnnotation 子类化并添加了NSString 和UIImage,因此我可以将这些元素传递给MKPinAnnotationView。该字符串传入MKPinAnnotationView 罚款,但图像属性作为空值传入。我不知道为什么。我会说有一次在我的手机上运行此代码时,图像出现在针上一次,所以也许我有内存泄漏?我编译的其他 1000 次图像在日志中显示为 null 并且没有出现。任何帮助表示赞赏。
这是我的子类 customMapAnnotation.h:
@interface customMapAnnotation : MKPointAnnotation
@property (strong, nonatomic) NSString *restaurantID;
@property (strong, nonatomic) UIImage *restaurantIcon;
@end
这里是我的customMapAnnotation 创建的地方。我正在使用 Parse.com,所以这里有一个将 PFFile 转换为 UIImage 的语句。如果我检查日志,我确认 foodPoint.restaurantIcon 在运行时有一个值。
customMapAnnotation *foodPoint = [[customMapAnnotation alloc] init];
foodPoint.coordinate = spot;
foodPoint.restaurantID = [object objectId];
foodPoint.title = [object objectForKey:@"restaurantName"];
foodPoint.subtitle = [object objectForKey:@"cuisineType"];
leftIcon = [object objectForKey:@"restaurantImage"];
[leftIcon getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
image = [UIImage imageWithData:data];
// image can now be set on a UIImageView
foodPoint.restaurantIcon = image;
}
}];
[self.mainMap addAnnotation:foodPoint];
现在这里是我的MKPinAnnotationView 的所有代码。请注意,我运行NSLog 来确认fp.restaurantIcon 是否有值,并且它为空。我知道NSLog 的这种使用并不优雅,但它应该返回图像的内存位置。它返回为空。同样,我的其他自定义属性 fp.restaurantId 工作正常。
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
static NSString *identifier = @"RoutePinAnnotation";
if (annotation == mapView.userLocation)
{
return nil;
}
else
{
MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:identifier];
pinView.animatesDrop=NO;
pinView.canShowCallout=YES;
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
//cast annotation parameter to our class so compiler understands...
customMapAnnotation *fp = (customMapAnnotation *)annotation;
NSLog(fp.restaurantIcon);
//get restaurantID from the annotation parameter ("fp")...
[rightButton setTitle:fp.restaurantID forState:UIControlStateNormal];
[rightButton addTarget:self
action:@selector(buttonMethod:)
forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;
//NSLog(fp.restaurantIcon);
UIImageView *profileIconView = [[UIImageView alloc] initWithImage:fp.restaurantIcon];
profileIconView.frame = CGRectMake(0,0,31,31);
pinView.leftCalloutAccessoryView = profileIconView;
return pinView;
}
}
这是我的didSelectAnnotationView:
- (void) mainMap:(MKMapView *)mainMap didSelectAnnotationView:(MKAnnotationView *)view
{
if ([view.annotation isKindOfClass:[chwMapAnnotation class]])
{
chwMapAnnotation *fp = (chwMapAnnotation *)view.annotation;
UIImageView *profileIconView = [[UIImageView alloc] initWithImage:fp.restaurantIcon];
profileIconView.frame = CGRectMake(0,0,31,31);
view.leftCalloutAccessoryView = profileIconView;
}
}
【问题讨论】:
-
我认为这可能是由您的背景图像加载器 getDataInBackgroundWithBlock 引起的。您可以在 UIImageView *profileIconView 中设置断点并检查 fp.restaurantIcon 是否为 nil。可能在您创建 MKPinAnnotationView 时,restaurantIcon 仍为 nil。
-
你可以试试这个,UIImageView *profileIconView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 37, 31)]; profileIconView.clipsToBounds = YES; profileIconView.image = fp.restaurantIcon; pinView.leftCalloutAccessoryView = profileIconView;
标签: ios objective-c uiimage parse-platform mkannotationview