有人可以告诉我我在做什么错吗?我使用这种方法来绘制图像。但代码中的某些内容似乎无法正确释放,因为每次我调用此方法时,我的应用程序内存占用量都会增加。iPhone:内存管理
在这里,我提供了增加我的记忆力的方法。
-(void)imagemaking
{
UIGraphicsEndImageContext();
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
SBJSON *parser = [[SBJSON alloc] init];
statuses = [parser objectWithString:responseString
error:nil];
[parser release];
networkConnection.image = [UIImage imageNamed:@"NetworkTrans1.png"];
[updateTimeClock setImage:[UIImage imageNamed:@"GreenClock.png"] forState:UIControlStateNormal];
NSArray *segment = [[NSArray alloc]init];
segment = [statuses valueForKey:@"Segments"];
int linewidth = 3;
CGSize polyimagesize = CGSizeMake(self.mapView.frame.size.width , self.mapView.frame.size.height);
UIGraphicsBeginImageContextWithOptions(polyimagesize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, linewidth);
CGContextSetAlpha(context, 0.6);
for(NSDictionary *route in segment) {
NSString *locations = [route valueForKey:@"Locations"];
if (locations && ([locations length]/16 > 1)) {
UIColor *color = [UIColor blueColor];
CGContextSetStrokeColorWithColor(context, color.CGColor);
for (int i = 0; i <= locations.length - 32; i += 32) {
CLLocationCoordinate2D coordinates;
coordinates.latitude = hexDecode_iPhone([locations substringWithRange:NSMakeRange(i, 16)]);
coordinates.longitude = hexDecode_iPhone([locations substringWithRange:NSMakeRange(i+16, 16)]);
CGPoint point = [mapView convertCoordinate:coordinates toPointToView:self.mapView];
if (i == 0)
CGContextMoveToPoint(context, point.x, point.y);
else
CGContextAddLineToPoint(context, point.x, point.y);
}
CGContextStrokePath(context);
}
}
madeimage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[responseString release];
[segment release];
}
按我的调查,因为CoreGraphics.But的内存占用增加,我不知道如何减少内存使用或哪个对象是增加内存usage.Please提供指导线。
在此先感谢!
我想,首先你应该经历整个代码。我在函数的末尾释放段。我在仪器的帮助下检查整个10次,我发现在我的应用程序中没有泄漏。 – Nit 2012-03-21 07:23:30
你正在释放别的东西。 – jacekmigacz 2012-03-21 07:25:13
@Nit:你释放'valueForKey:'返回的数组,而不是'alloc'和'init'返回的数组。它们是两个独立的数组对象,并且您立即用第二个数组替换第一个,从而泄漏第一个。即使有另外的泄漏(如果有的话,我不能在你显示的代码中看到它),jacekmigacz的答案是正确的。 – 2012-03-24 17:05:52