0

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

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

1 回答 1

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

于 2013-11-08T07:51:40.173 回答