相当迟了回应,但我想其他人仍然会有兴趣的
在MKAnnotationView子类:
首先,定义一些尺寸:
如果您希望背景是一个图像,而不仅仅是一个颜色
self.frame = CGRectMake(0, 0, ckImageWidth, ckImageHeight);
:
self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"map_checkin"]];
然后做一个占位符图像
然后定义注释视图的框架
CGRect checkInPictureRect = CGRectMake(kBorder, kBorder, ckImageWidth - 9 , ckImageWidth - 9);
UIView *checkInPictureView = [[UIView alloc]initWithFrame:checkInPictureRect];
然后乐趣开始:
// Crop image
UIImage *croppedImage = [ImageHelper centerCropImage:image];
// Resize image
CGSize checkInPictureSize = CGSizeMake(checkInPictureRect.size.width*1.5, checkInPictureRect.size.height*1.5);
UIGraphicsBeginImageContext(checkInPictureSize);
[croppedImage drawInRect:CGRectMake(0, 0, checkInPictureRect.size.width*1.5, checkInPictureRect.size.height*1.5)];
UIImage* resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *imageView = [[UIImageView alloc] initWithImage:resizedImage];
imageView.frame = checkInPictureView.bounds;
[checkInPictureView addSubview:imageView];
[self addSubview:checkInPictureView];
// Counter
UIView *counterView = [[UIView alloc]initWithFrame:CGRectMake(45, -2, 15, 15)];
counterView.opaque = YES;
(checkIn.isNow) ? [counterView setBackgroundColor:[UIColor enloopBlue]] : [counterView setBackgroundColor:[UIColor enloopGreen]];
counterView.layer.cornerRadius = 8;
self.counterLabel = [[UILabel alloc] init];
self.counterLabel.frame = CGRectMake(4, 2, 10, 10);
if (self.count >= 10) {
counterView.frame = CGRectMake(45, -2, 18, 18);
self.counterLabel.frame = CGRectMake(3, 3, 12, 12);
}
[self.counterLabel setTextColor:[UIColor whiteColor]];
[self.counterLabel setFont:[UIFont fontWithName: @"Trebuchet MS" size: 11.0f]];
[self.counterLabel setText:[[NSString alloc] initWithFormat:@"%lu", (unsigned long)self.count]];
[counterView addSubview:self.counterLabel];
[counterView bringSubviewToFront:self.counterLabel];
[self addSubview:counterView];
对于centerCropImage
帮手,没有什么特别的:
+ (UIImage *)centerCropImage:(UIImage *)image {
// Use smallest side length as crop square length
CGFloat squareLength = MIN(image.size.width, image.size.height);
// Center the crop area
CGRect clippedRect = CGRectMake((image.size.width - squareLength)/2, (image.size.height - squareLength)/2, squareLength, squareLength);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], clippedRect);
UIImage * croppedImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return croppedImage;
}
我知道有相当多的事情,以改善,但到那时,我希望这会帮助别人。 :)
感谢Adela的建议,绝对有帮助! – 2015-06-15 20:54:30
欢迎:) @ emn.mun – Adela 2015-06-16 15:18:30