我有一个在 ASIHTTPRequest 进程中使用的 NSMutableArray。数据加载完成后,NSMutableArray 存储信息。当我将数据添加为
[MyArray addObject];
我没有任何错误。但是,当我将数据插入为
[MyArray insertObject:[UIImage imageWithData:data] atIndex:buttonTag];
我有 malloc 错误或索引超出范围异常问题。我认为这是线程安全故障。有什么解决办法吗?
编辑:在 appdelegate.h
@interface{
NSMutableArray *imageArray;
}
在 appdelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
imageArray = [[NSMutableArray alloc] init];
return YES;
}
在 AsyncImageView.h
@interface{
AppDelegate *delegate
}
AsyncImageView.m
- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[delegate.imageArray insertObject:[UIImage imageWithData:data] atIndex:buttonTag];
}