UIImage returns 第一次为 null,直到重新加载?
UIImage returns null first time, until reloaded?
我从 url 中检索 NSData
,然后尝试将其设置为图像。
在控制台中它 returns 为空,但是当我请求数据并再次设置图像 时,图像会加载吗?
为什么必须执行两次才能加载?
这是我用来获取图片的两种方法。
-(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???
}
随着数据的递增加载,connection:didReceiveData:
方法被重复调用。您可能需要 connectionDidFinishLoading:
方法。
顺便说一句,您可能仍然需要 connection:didReceiveData:
,但您无需尝试创建图像,只需将新数据附加到缓冲区即可。然后在 connectionDidFinishLoading:
方法中,获取缓冲区并创建图像。
示例代码,但请根据需要添加错误处理:
@property (strong, nonatomic) NSMutableData *data;
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
self.data = [NSMutableData data];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.data appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
self.userPicture = [UIImage imageWithData:self.data];
}
我从 url 中检索 NSData
,然后尝试将其设置为图像。
在控制台中它 returns 为空,但是当我请求数据并再次设置图像 时,图像会加载吗?
为什么必须执行两次才能加载?
这是我用来获取图片的两种方法。
-(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???
}
随着数据的递增加载,connection:didReceiveData:
方法被重复调用。您可能需要 connectionDidFinishLoading:
方法。
顺便说一句,您可能仍然需要 connection:didReceiveData:
,但您无需尝试创建图像,只需将新数据附加到缓冲区即可。然后在 connectionDidFinishLoading:
方法中,获取缓冲区并创建图像。
示例代码,但请根据需要添加错误处理:
@property (strong, nonatomic) NSMutableData *data;
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
self.data = [NSMutableData data];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.data appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
self.userPicture = [UIImage imageWithData:self.data];
}