gpt4 book ai didi

ios - 如何在来自推送通知的 URL 的 ImageView 中显示图像

转载 作者:行者123 更新时间:2023-11-29 03:30:35 24 4
gpt4 key购买 nike

我想根据 URL 在 View 中显示图片,但 url 作为推送通知出现,我正在正确获取所有 PNS,也成功获取了图片 URL,但图片显示不正确。下面是我在收到新推送通知时使用的代码,我正在打开该通知并调用方法,该方法位于我要设置图像的另一个 View 中。

- (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];
});
});
}

最佳答案

试试这个

-(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 相关任务

关于ios - 如何在来自推送通知的 URL 的 ImageView 中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19853357/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com