我想从不同的网址中获取许多图像并将其设置为图像的按钮。 我试图这样做,就像上面所示,但没有任何事情发生。当我访问此视图控制器时,它没有任何按钮图像,也没有显示横幅视图....
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
NSURL *url = [NSURL URLWithString: [pictureUrl objectAtIndex:i]];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
dispatch_async(dispatch_get_main_queue(), ^(void){
UIImage *img1 = [[UIImage alloc]initWithData:data];
img2.image = img1;
[bt setBackgroundImage:img2.image forState:UIControlStateNormal];
});
});
答案 0 :(得分:0)
我建议您使用支持图像缓存的库。例如,我使用AFNetworking作为我的一个项目,它很棒。它会自动为您处理背景。在我的情况下,我需要一个库,当我开始新的请求时它自动取消请求,它对我有用。您可以看到代码here。您可以从另一个线程中看到类似的solution,如下所示:
AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Response: %@", responseObject);
_imageView.image = responseObject;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Image error: %@", error);
}];
[requestOperation start];
希望它对你有所帮助。
答案 1 :(得分:0)
恕我直言,您应该使用 async
和sync
对,内部块应与第一个同步,以便在下载和提供数据时拍摄图片。另外,不要忘记处理错误:
试试这样:
// Show HUD here
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void)
{
NSError* error;
NSURL *url = [NSURL URLWithString: [pictureUrl objectAtIndex:i]];
NSData *data = [[NSData alloc] initWithContentsOfURL:url options:0 error:&error];
dispatch_sync(dispatch_get_main_queue(), ^(void){
if(!error && data)
{
UIImage *img1 = [[UIImage alloc] initWithData:data];
img2.image = img1;
[bt setBackgroundImage:img2.image forState:UIControlStateNormal];
/*
//----OR----
[bt setBackgroundImage:img1 forState:UIControlStateNormal];
*/
}
else
{
// Do error handling here
}
// Hide HUD here
});
});
希望它有所帮助!
答案 2 :(得分:0)
imageView.image = [UIImage imageWithData:yourDefaultImgUrl];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *imageData = [NSData dataWithContentsOfURL:yourImageUrl];
if (imageData){
dispatch_async(dispatch_get_main_queue(), ^{
imageView.image = [UIImage imageWithData:imageData];
});
}
});
希望有所帮助。
答案 3 :(得分:-1)
尝试此修改,
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
NSURL *url = [NSURL URLWithString: [pictureUrl objectAtIndex:i]];
NSError *error;
NSData *data = [NSData dataWithContentsOfURL:url options:0 error:&error];
if(error)
{
NSLog(@"Error: %@", [error localizedDescription]);
}
else
{
UIImage *img1 = [[UIImage alloc]initWithData:data];
[bt setBackgroundImage:img1 forState:UIControlStateNormal];
}
// dispatch_async(dispatch_get_main_queue(), ^(void){
//img2.image = img1;
// });
});
希望有所帮助。
答案 4 :(得分:-1)
试试这个
dispatch_queue_t myQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(myQueue, ^{
NSString *ImageURL = @"yourURL.jpg";
NSData *imageData;
if (ImageURL == nil || ImageURL == (id)[NSNull null] || [[NSString stringWithFormat:@"%@",ImageURL] length] == 0 || [[[NSString stringWithFormat:@"%@",ImageURL] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0)
{
}
else
{
imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]];
}
dispatch_async(dispatch_get_main_queue(), ^{
if (ImageURL == nil || ImageURL == (id)[NSNull null] || [[NSString stringWithFormat:@"%@",ImageURL] length] == 0 || [[[NSString stringWithFormat:@"%@",ImageURL] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0)
{
imageView.image=[UIImage imageNamed:@"photo_frame_noimage.png"];
}
else if (imageData == nil || imageData == (id)[NSNull null] || [[NSString stringWithFormat:@"%@",imageData] length] == 0 || [[[NSString stringWithFormat:@"%@",imageData] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0)
{
imageView.image=[UIImage imageNamed:@"photo_frame_noimage.png"];
}
else
{
imageView.image=[UIImage imageWithData:imageData];
}
});
});