我想基于URL在视图中显示图像,但是url作为推送通知,我正在获得所有PNS,也成功获得图像URL但图像显示不正确。下面的代码,我正在使用当我得到新的推送通知我打开通知和调用方法whch是在另一个视图,我想设置图像。
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
if (application.applicationState == UIApplicationStateActive)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Did receive a Remote Notification" message:[NSString stringWithFormat:@"You Have a Notification :\n%@",userInfo[@"aps"][@"alert"]]delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
NSLog(@"Payload: %@", userInfo);
imageURL = userInfo[@"aps"][@"alert"];
MainViewController *mv = [[MainViewController alloc] init];
[mv sshowansimage:imageURL]; // CALL METHOD OF ANTOHER CLASS WHERE I WANT TO SET IMAGE
}
现在这里是sshowansimage方法的代码,它是mainviewcontrriler类
-(void) sshowansimage:(NSString *) strImageURL{
NSURL *imageURL = [NSURL URLWithString:strImageURL];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
dispatch_async(dispatch_get_main_queue(), ^{
// Update the UI
imgeview.image = [UIImage imageWithData:imageData];
});
});
}
答案 0 :(得分:1)
试试这个
-(void) sshowansimage:(NSString *) strImageURL{
NSURL *imageURL = [NSURL URLWithString:strImageURL];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
[self performSelectorOnMainThread:@selector(showImage:) withObject:imageData waitUntilDone:YES];
});
}
-(void)showImage:(NSData*)imageAsData
{
imgeview.image = [UIImage imageWithData:imageAsData];
}
始终在主线程中执行与UI相关的任务