gpt4 book ai didi

ios - 如何更改集群的图标

转载 作者:行者123 更新时间:2023-11-29 02:31:34 26 4
gpt4 key购买 nike

我有一个基于 map 的应用程序,使用 Google map 的 iOS SDK。我需要更改集群图标,因为它在集群上显示圆形图像。就像在 googlemap SDK 中创建的圆圈一样,其中显示了标记的数量。我如何更改集群的图标,因为它显示棕色圆圈?请帮助我,我需要着急啊。下面是集群创建代码

- (UIImage*) generateClusterIconWithCount:(NSUInteger)count {

int diameter = 40;
float inset = 3;

CGRect rect = CGRectMake(0, 0, diameter, diameter);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);

CGContextRef ctx = UIGraphicsGetCurrentContext();

// set stroking color and draw circle
[[UIColor colorWithRed:1 green:1 blue:1 alpha:0.8] setStroke];
[[UIColor brownColor] setFill];

CGContextSetLineWidth(ctx, inset);

// make circle rect 5 px from border
CGRect circleRect = CGRectMake(0, 0, diameter, diameter);
circleRect = CGRectInset(circleRect, inset, inset);

// draw circle
CGContextFillEllipseInRect(ctx, circleRect);
CGContextStrokeEllipseInRect(ctx, circleRect);

CTFontRef myFont = CTFontCreateWithName( (CFStringRef)@"Helvetica-Bold", 18.0f, NULL);

NSDictionary *attributesDict = [NSDictionary dictionaryWithObjectsAndKeys:
(__bridge id)myFont, (id)kCTFontAttributeName,
[UIColor whiteColor], (id)kCTForegroundColorAttributeName, nil];

// create a naked string
NSString *string = [[NSString alloc] initWithFormat:@"%lu", (unsigned long)count];

NSAttributedString *stringToDraw = [[NSAttributedString alloc] initWithString:string
attributes:attributesDict];

// flip the coordinate system
CGContextSetTextMatrix(ctx, CGAffineTransformIdentity);
CGContextTranslateCTM(ctx, 0, diameter);
CGContextScaleCTM(ctx, 1.0, -1.0);

CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)(stringToDraw));
CGSize suggestedSize = CTFramesetterSuggestFrameSizeWithConstraints(
frameSetter, /* Framesetter */
CFRangeMake(0, stringToDraw.length), /* String range (entire string) */
NULL, /* Frame attributes */
CGSizeMake(diameter, diameter), /* Constraints (CGFLOAT_MAX indicates unconstrained) */
NULL /* Gives the range of string that fits into the constraints, doesn't matter in your situation */
);
CFRelease(frameSetter);

//Get the position on the y axis
float midHeight = diameter;
midHeight -= suggestedSize.height;

float midWidth = diameter / 2;
midWidth -= suggestedSize.width / 2;

CTLineRef line = CTLineCreateWithAttributedString(
(__bridge CFAttributedStringRef)stringToDraw);
CGContextSetTextPosition(ctx, midWidth, 12);
CTLineDraw(line, ctx);

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return image;
}

最佳答案

检查这个!

-(UIImage*) generateClusterIconWithCount:(NSUInteger)count {

int diameter = 40;
float inset = 3;



CGRect rect = CGRectMake(0, 0, diameter, diameter);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);

CGContextRef ctx = UIGraphicsGetCurrentContext();

// set stroking color and draw circle
// [[UIColor colorWithRed:1 green:1 blue:1 alpha:0.8] setStroke];
// [[UIColor brownColor] setFill];

CGContextSetLineWidth(ctx, inset);

// make circle rect 5 px from border
CGRect circleRect = CGRectMake(0, 0, diameter, diameter);
circleRect = CGRectInset(circleRect, inset, inset);
UIImage *titleImage = [UIImage imageNamed:@"images.jpg"];
[titleImage drawInRect:circleRect];
// draw circle
//CGContextFillEllipseInRect(ctx, circleRect);
// CGContextStrokeEllipseInRect(ctx, circleRect);

CTFontRef myFont = CTFontCreateWithName( (CFStringRef)@"Helvetica-Bold", 18.0f, NULL);

NSDictionary *attributesDict = [NSDictionary dictionaryWithObjectsAndKeys:
(__bridge id)myFont, (id)kCTFontAttributeName,
[UIColor whiteColor], (id)kCTForegroundColorAttributeName, nil];

// create a naked string
NSString *string = [[NSString alloc] initWithFormat:@"%lu", (unsigned long)count];

NSAttributedString *stringToDraw = [[NSAttributedString alloc] initWithString:string
attributes:attributesDict];

// flip the coordinate system
CGContextSetTextMatrix(ctx, CGAffineTransformIdentity);
CGContextTranslateCTM(ctx, 0, diameter);
CGContextScaleCTM(ctx, 1.0, -1.0);

CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)(stringToDraw));
CGSize suggestedSize = CTFramesetterSuggestFrameSizeWithConstraints(
frameSetter, /* Framesetter */
CFRangeMake(0, stringToDraw.length), /* String range (entire string) */
NULL, /* Frame attributes */
CGSizeMake(diameter, diameter), /* Constraints (CGFLOAT_MAX indicates unconstrained) */
NULL /* Gives the range of string that fits into the constraints, doesn't matter in your situation */
);
CFRelease(frameSetter);

//Get the position on the y axis
float midHeight = diameter;
midHeight -= suggestedSize.height;

float midWidth = diameter / 2;
midWidth -= suggestedSize.width / 2;

CTLineRef line = CTLineCreateWithAttributedString(
(__bridge CFAttributedStringRef)stringToDraw);
CGContextSetTextPosition(ctx, midWidth, 12);
CTLineDraw(line, ctx);

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return image;
}

关于ios - 如何更改集群的图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26794326/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com