我有我的实例变量声明:
@interface TimelineTableViewController ()
@property (strong, nonatomic) NSMutableArray *avatars;
@end
然后是我的 block :
void UIImageFromURL(NSURL *URL, NSString *key, void(^imageBlock)(UIImage *image), void(^errorBlock)(void)) {
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
NSData *data = [[NSData alloc] initWithContentsOfURL:URL];
UIImage *image = [[UIImage alloc] initWithData:data];
dispatch_async( dispatch_get_main_queue(), ^(void){
if (image != nil) {
[self.avatars setObject:image forKey:key];
NSLog(@"%@", self.avatars);
imageBlock( image );
} else {
errorBlock();
}
});
});
}
我想做的是将下载的图像缓存到实例数组中,以避免重新下载每个重新创建的 TableCell。但是,我收到错误:
Use of undeclared identifier 'self'
我这样称呼我的 block ,如果有帮助的话:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TweetCell *cell = (TweetCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSDictionary *tweetObject = _tweets[indexPath.row];
NSDictionary *userObject = tweetObject[@"user"];
NSString *avatarURL = userObject[@"profile_image_url"];
avatarURL = [avatarURL stringByReplacingOccurrencesOfString:@"_normal"
withString:@""];
NSURL *imageURL = [NSURL URLWithString:avatarURL];
UIImageFromURL(imageURL, avatarURL, ^(UIImage *image) {
[cell.avatarImage setImage:image];
}, ^{
NSLog(@"Failed to fetch profile picture.");
});
return cell;
}
最佳答案
在你的 block 之前,使用这行代码来修改你的self
:
// Weak self
__weak MyViewController *weakSelf = self;
并像这样调用你的变量:
void UIImageFromURL(NSURL *URL, NSString *key, void(^imageBlock)(UIImage *image), void(^errorBlock)(void)) {
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
NSData *data = [[NSData alloc] initWithContentsOfURL:URL];
UIImage *image = [[UIImage alloc] initWithData:data];
dispatch_async( dispatch_get_main_queue(), ^(void){
if (image != nil) {
[weakSelf.avatars setObject:image forKey:key];
NSLog(@"%@", weakSelf.avatars);
imageBlock( image );
} else {
errorBlock();
}
});
});
}
您可以引用here关于block
的有力答案!
关于objective-c - 访问 block 内的实例变量 - Objective-C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23054997/