相當遲了迴應,但我想其他人仍然會有興趣的
在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