【问题标题】:Getting asynchronous Twitter Profile image iOS5获取异步 Twitter 个人资料图片 iOS5
【发布时间】:2012-06-18 12:25:19
【问题描述】:

无法弄清楚为什么 profileImage 没有粘住...(我添加 pImage 只是为了看看它是否与在块内有关...不是)。

profileImage 只是一个普通的综合属性

@property (strong, nonatomic) UIImageView *profileImage;

-(void)fetchProfileImage
{
    __block UIImage *pImage;

    dispatch_async(dispatch_get_global_queue(0, 0), ^
    {
        NSString *url_ = [NSString stringWithFormat:@"http://api.twitter.com/1/users/profile_image/%@", userName];

        TWRequest *fetchUserImageRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:url_] parameters:nil requestMethod:TWRequestMethodGET];

        [fetchUserImageRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) 
         {
             if ([urlResponse statusCode] == 200) 
             {
                 dispatch_async(dispatch_get_main_queue(), ^(void) 
                {
                    NSLog(@"[UIImage imageWithData:responseData] = %@", [UIImage imageWithData:responseData]);

                    pImage = [UIImage imageWithData:responseData];

                    NSLog(@"pImage = %@", pImage);

                    self.profileImage.image = pImage;

                    NSLog(@"self.profileImage.image = %@", self.profileImage.image);

                });
             }
         }];
    });
}

日志:

[UIImage imageWithData:responseData] = <UIImage: 0x2685d0>
pImage = <UIImage: 0x2685d0>
self.profileImage.image = (null)

【问题讨论】:

  • 我们看不到self.profileImage的属性。我猜这就是问题所在。此外,您不需要在循环之外定义 pImage 。另一个问题,响应返回时“self”是否存在?
  • @PauldeLange 你的意思是怎么看不到属性? self 正确返回。
  • profileImage 有效吗?也许它已在其他地方设置为零?如果 self 或 profileImage 为 nil,则会发生这种情况。我假设 profileImage 是一个 UIImageView,你的视图加载了吗?
  • dispatch_get_global_queue(0, 0) 可以替换为 dispatch_get_main_queue() 以确保您也在 UI 线程上。
  • 事实上,这里有些问题。没有理由在调用函数的不同线程中创建 TWRequest 对象。

标签: iphone ios5 twitter objective-c-blocks


【解决方案1】:

原来我在创建父对象时没有初始化 UIImageView

tweet.profileImage = [[UIImageView alloc] init];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-15
    • 1970-01-01
    • 2012-07-18
    • 2015-05-07
    • 2015-05-06
    • 2011-10-08
    相关资源
    最近更新 更多