我有 NSURLConnection 从 URL 下载 PNG 文件。这是代码:
-(void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData {
if (data==nil) { data = [[NSMutableData alloc] initWithCapacity:2048]; }
[data appendData:incrementalData];
}
-(void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
[self saveItLocal];
connection = nil;
data = nil;
}
-(void)saveItLocal {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *docs = [paths objectAtIndex:0];
NSString* path = [docs stringByAppendingFormat:[NSString stringWithFormat:@"/%@.png",self.downloadType]];
NSData* imageData = [NSData dataWithData:UIImagePNGRepresentation([UIImage imageWithData:data])];
NSError *writeError = nil;
[imageData writeToFile:path options:NSDataWritingAtomic error:&writeError];
if(writeError!=nil) {
NSLog(@"%@: Error saving image: %@", [self class], [writeError localizedDescription]);
}
[[NSNotificationCenter defaultCenter] postNotificationName:@"readyWithGraphics" object:self];
imageData = nil;
}
为什么这个对象仍然存在并存在于记忆中?