Ios UIView转换为UIImage时如何使用自动布局
CPOAnnotationView是UIView的子视图,我想将CPOAnnotationView转换为UIImageIos UIView转换为UIImage时如何使用自动布局,ios,objective-c,uiview,autolayout,uiimage,Ios,Objective C,Uiview,Autolayout,Uiimage,CPOAnnotationView是UIView的子视图,我想将CPOAnnotationView转换为UIImage - (UIImage*)getImage:(CPOAnnotationViewSize)size { CPOAnnotationView* annotationView = [[CPOAnnotationView alloc] initWithFrame:CGRectZero]; [annotationView setNeedsLayout]; [ann
- (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()方法创建地图注释,并将注释添加到地图视图
CPOAnnotation *annotation = [[CPOAnnotation alloc] initWithCoordinate: coordinate image:[cpoMarker getImage:CPOAnnotationViewSize_Big] content:[cpoMarker content]];
[self.mapView addAnnotation:annotation];
并在MapView回调方法调用时向MapView添加注释:
- (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;
}
}
-(MAAnnotationView*)地图视图:(MAMapView*)地图视图用于注释:(id)注释{
if([annotation IsKindof类:[CPOAnnotation类]]){
CPOAnnotation*CPOAnnotation=注释;
NSString*cpoReuseIndetifier=[NSString stringWithFormat:@“%@”、@“cpoReuseIndetifier”、[CPOAnotation content]];
MaAnotationView*annotationView=[mapView dequeueReusableAnnotationViewWithIdentifier:CPoreUseIndifier];
如果(注释视图==nil){
annotationView=[[MAAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:CPoreUseIndifier];
}
annotationView.image=[cpoannotationimage];
返回注释视图;
}
}