问题描述
CPOAnnotationView 是 UIView 的一个子视图,我想把 CPOAnnotationView 转换成 UIImage。
- (UIImage*)getimage:(CPOAnnotationViewSize)size {
CPOAnnotationView* annotationView = [[CPOAnnotationView alloc] initWithFrame:CGRectZero];
[annotationView setNeedsLayout];
[annotationView layoutIfNeeded];
CGSize s = annotationView.bounds.size;
UIGraphicsBeginImageContextWithOptions(s,NO,[UIScreen mainScreen].scale);
[annotationView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage*image = UIGraphicsGetimageFromCurrentimageContext();
UIGraphicsEndImageContext();
return image;
}
问题是 CPOAnnotationView 具有灵活的宽度,可以包裹它的内容。所以我将 CPOAnnotationView 的框架设置为 CGRectZero。我搜索了stackoverflow,有人说如果你想将uiview转换为uiimage。 uiview 的宽度或高度不能为零。但是我的 CPOAnnotationView 具有灵活的宽度,在设置框架之前计算宽度很复杂。下面是 CPOAnnotationView 的代码
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]){
self.translatesAutoresizingMaskIntoConstraints = NO;
_titleView = [[UIView alloc] initWithFrame:CGRectZero];
_titleView.translatesAutoresizingMaskIntoConstraints = NO;
_titleView.backgroundColor = [UIColor whiteColor];
[_titleView layoutIfNeeded];
[self addSubview:_titleView];
NSMutableArray* titleViewConstraints = [NSMutableArray arrayWithCapacity:0];
[titleViewConstraints addobject:[NSLayoutConstraint constraintWithItem:_titleView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f]];
[titleViewConstraints addobject:[NSLayoutConstraint constraintWithItem:_titleView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0f constant:34.0f]];
// [titleViewConstraints addobject:[NSLayoutConstraint constraintWithItem:_nameLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:_avatarButton attribute:NSLayoutAttributeRight multiplier:1.0f constant:kInnerMesListCellAvatarRightMargin]];
// [titleViewConstraints addobject:[NSLayoutConstraint constraintWithItem:_nameLabel attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:_dateLabel attribute:NSLayoutAttributeLeft multiplier:1.0f constant:-10.0]];
[self addConstraints:titleViewConstraints];
UILabel* label = [[UILabel alloc] initWithFrame:CGRectZero];
label.translatesAutoresizingMaskIntoConstraints = NO;
label.backgroundColor = [UIColor orangeColor];
label.textAlignment = NSTextAlignmentCenter;
label.text = @"1000000";
label.font = [UIFont systemFontOfSize:16];
label.textColor = [UIColor blackColor];
[label layoutIfNeeded];
[_titleView addSubview:label];
NSMutableArray* labelConstraints = [NSMutableArray arrayWithCapacity:0];
[labelConstraints addobject:[NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:_titleView attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f]];
[labelConstraints addobject:[NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0f constant:34.0f]];
[labelConstraints addobject:[NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:_titleView attribute:NSLayoutAttributeLeft multiplier:1.0f constant:10.0f]];
[labelConstraints addobject:[NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:_titleView attribute:NSLayoutAttributeRight multiplier:1.0f constant:-10.0f]];
[_titleView addConstraints:labelConstraints];
}
return self;
}
下面是我如何调用getimage()方法,我使用getimage()方法创建了一个地图注解,并将注解添加到MapView中
CPOAnnotation *annotation = [[CPOAnnotation alloc] initWithCoordinate: coordinate image:[cpoMarker getimage:CPOAnnotationViewSize_Big] content:[cpoMarker content]];
[self.mapView addAnnotation:annotation];
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation {
if([annotation isKindOfClass:[CPOAnnotation class]]) {
CPOAnnotation* cpoAnnotation = annotation;
Nsstring *cpoReuseIndetifier = [Nsstring stringWithFormat:@"%@_%@",@"cpoReuseIndetifier",[cpoAnnotation content]];
MAAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:cpoReuseIndetifier];
if (annotationView == nil) {
annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:cpoReuseIndetifier];
}
annotationView.image = [cpoAnnotation image];
return annotationView;
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)