0

我正在创建一个具有字典作为属性的追逐类的单个实例。当我向字典中添加元素时,我可以看到字典键数的变化。但是当我访问时,它会触发崩溃,并显示如下消息:

[__NSCFString 连接:didReceiveData:];

[__NSCFArray reqmap]; ETC..

基本上,我将请求 url 与NSData实例映射,以便可以异步地正确映射和附加响应。

代码:

-(id)init
{
    self = [super init];
    if (self) 
    {
        self.cacheDict = [[NSMutableDictionary alloc] initWithCapacity:20];
        self.reqmap = [[NSMutableDictionary alloc] initWithCapacity:20];
    }
    return self;
}

+ (CommunityImageCache*)getSharedCommunityImageCache
{
    if (sharedCommunityImageCacheInstance == nil) {
        sharedCommunityImageCacheInstance = [[super allocWithZone:NULL] init];
    }
    return sharedCommunityImageCacheInstance;
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSString* keyurl = [[[connection currentRequest] URL] absoluteString];
 //crash point >>>>
    NSMutableData* tempdata = (NSMutableData*)[self.reqmap objectForKey:keyurl];
    [tempdata appendData:data];
    
}


- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection 
{
    NSString* keyurl = [[[theConnection currentRequest] URL] absoluteString];
    NSMutableData* tempdata = [self.reqmap objectForKey:keyurl];
    UIImage* img = [UIImage imageWithData:tempdata];  
    [self.cacheDict setObject:img forKey:[@"image:\\\\public\\" stringByAppendingString:keyurl]];
    [self.reqmap removeObjectForKey:keyurl];
}


-(UIImage *)getImageFromUrl:(NSString *)url
{
    UIImage* image = nil;
    
        image = [self.cacheDict objectForKey:url];
        if (!image) 
        {
            image = [UIImage imageNamed:@"defaultProfile.png"];
            //dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
            
            NSString* localUrl = [NSString stringWithString:url];
            NSString* finurl = [localUrl substringFromIndex:[@"image:\\\\public\\" length]]; 
            UIImage* img = nil;
            NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:finurl]
                                                     cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                 timeoutInterval:60.0];
            
            NSMutableData* data = [[NSMutableData alloc] init];
            NSURLConnection* connect = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
            NSString* keyurl = [[[connect currentRequest] URL] absoluteString];
            [self.reqmap setObject:data forKey:keyurl];
}
}

急需,提前致谢。

4

2 回答 2

0

好吧,reqmap 应该包含 NSMutableData 对象,对吧?他们需要实例化,例如: [self.reqmap setValue:[[NSMutableData alloc] init] forKey:@"TheKey"];

于 2012-07-05T20:23:55.507 回答
0

您还应该实施

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 

当你使用 NSURLConnection 时。我在上面的代码中没有看到它。然后你应该分配/重置这个函数中的数据。

您看到的实际错误是什么?听起来 reqmap 不是 NSDictionary(尽管它在您上面的代码中),或者您正在检索的对象不是 NSMutableData。

总的来说,实施:

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
于 2012-07-05T22:18:57.700 回答