问候,

我想在地图上绘制一个圆。该项目独立工作的所有独立的部分,但是当我把它们放在一起它打破了。

设置我的UI在我viewDidLoad中,保留大部分。

我然后使用触摸事件来调用我的刷新映射方法:

-(void)refreshMap{

NSString *thePath = [NSString stringWithFormat:@"http://maps.google.com/staticmap?center=%f,%f&zoom=%i&size=640x640&maptype=hybrid",viewLatitude, viewLongitude, zoom];
NSURL *url = [NSURL URLWithString:thePath];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *mapImage = [[UIImage alloc] initWithData:data];

mapImage = [self addCircle:(mapImage) influence:(70) latCon:(320) lonCon:(320)];
NSLog(@"-- mapimageview retaincount %i",[mapImage retainCount]);    

mapImageView.image = mapImage;
[mapImage release];}

设置这样它会加载用圆圈一次地图,但如果地图被再次刷新它崩溃。

如果我注释掉mapImage释放它的作品多次,但会导致内存泄漏。

在addCircle方法我使用:

-(UIImage *)addCircle:(UIImage *)img radius:(CGFloat)radius latCon:(CGFloat)lat lonCon:(CGFloat)lon{
int w = img.size.width;
int h = img.size.height; 
lon = h - lon;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);

//draw the circle
CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
CGRect leftOval = {lat- radius/2, lon - radius/2, radius, radius};
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 0.3);
CGContextAddEllipseInRect(context, leftOval);
CGContextFillPath(context);

CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);

return [UIImage imageWithCGImage:imageMasked];}

任何洞察/提醒是非常感谢!

有帮助吗?

解决方案

UIImage *mapImage = [[UIImage alloc] initWithData:data];
mapImage = [self addCircle:(mapImage) influence:(70) latCon:(320) lonCon:(320)];

这并不好。你失去了参考mapImage的内容,当你重新分配它在第二行。最简单的方法来解决这个问题,如果可能只需添加一个额外的变量,因此您可以跟踪两个图像的。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top