【发布时间】:2015-08-31 14:21:35
【问题描述】:
我从 url 中检索 NSData,然后尝试将其设置为图像。
在控制台中它返回 null,但是当我请求数据并再次设置图像时,图像会加载吗?
为什么要加载两次才能加载?
这是我用来获取图片的两种方法。
-(void)getUserPicture {
//Grab and upload user profile picture
imageData = [[NSMutableData alloc] init]; // the image will be loaded in here
NSString *urlString = [NSString stringWithFormat:@"http://graph.facebook.com/%@/picture?type=large", userId];
NSMutableURLRequest *urlRequest =
[NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:3];
NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest
delegate:self];
if (!urlConnection) NSLog(@"Failed to download picture");
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
userPicture = [UIImage imageWithData:data];
NSLog(@"%@",userPicture); //returns null in console first time, until reloaded???
}
【问题讨论】:
标签: ios objective-c uiimage nsurl