gpt4 book ai didi

ios - 如何从 GCD 返回 UIImage 类型

转载 作者:行者123 更新时间:2023-11-28 22:28:05 24 4
gpt4 key购买 nike

我有一个名为“ComLog”的“UIImage”返回类型方法。我想从此方法返回一个图像。在“ComLog”方法中,我使用 GCD 从数组中获取图像值。我使用以下代码,“NSLog(@"qqqqqqqqqqq %@", exiIco)”打印“图像”值,但 NSLog(@"qqqqqqqqqqq %@", exiIco);"不打印。这是详细信息:

-(UIImage*) ComLog
{
ExibitorInfo *currentExibitor100 = [[ExibitorInfo alloc] init];
currentExibitor100 = [self.exibitorsArray objectAtIndex:0];

imageQueueCompanyLogo = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(imageQueueCompanyLogo, ^
{
UIImage *imageCompanyLogo = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:[currentExibitor100 companyLogoURL]]]];
dispatch_async(dispatch_get_main_queue(), ^
{
self.exibitorIcoImageView.image = imageCompanyLogo;
exiIco = imageCompanyLogo;
NSLog(@"qqqqqqqqqqq %@", exiIco);
});
});
return exiIco;
}


- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *a = [self ComLog];
NSLog(@"It should be a image %@", a);
}

这里所有的属性都是全局声明的(在“Myclass.h”文件中)。我是Objective C的新手,知道的请回复。提前致谢。

最佳答案

您的代码片段中有很多错误,很难决定从哪里开始。

我建议暂时离开GCD,等你更有经验的时候再看看。

基本上,您想要从远程服务器加载图像。 NSURLConnection 为此提供了一个方便的方法,足以满足非常简单的用例:

+ (void)sendAsynchronousRequest:(NSURLRequest *)request queue:(NSOperationQueue *)queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler;/p>

您可以在此处找到文档:NSURLConnection Class Reference .

加载远程资源的推荐方法是在异步模式下使用 NSURLConnection 实现委托(delegate)。您可以在此处找到更多信息: URL Loading System Programming Guide - Using NSURL Connection

我还建议阅读 Conventions .

这是一个如何使用 sendAsynchronousRequest 的简短示例:

NSURL* url = [NSURL URLWithString:[currentExibitor100 companyLogoURL]];
NSMutableURLRequest* urlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue* queue = [[NSOperationQueue alloc] init];

[NSURLConnection sendAsynchronousRequest:urlRequest
queue:queue
completionHandler:^(NSURLResponse* response,
NSData* data,
NSError* error)
{
if (data) {
// check status code, and optionally MIME type
if ( [(NSHTTPURLResponse*)(response) statusCode] == 200 /* OK */) {
UIImage* image = [UIImage imageWithData:data];
if (image) {
dispatch_async(dispatch_get_main_queue(), ^{
self.exibitorIcoImageView.image = image;
});
} else {
NSError* err = [NSError errorWithDomain: ...];
[self handleError:err]; // execute on main thread!
}
}
else {
// status code indicates error, or didn't receive type of data requested
NSError* err = [NSError errorWithDomain:...];
[self handleError:err]; // execute on main thread!
}
}
else {
// request failed - error contains info about the failure
[self handleError:error]; // execute on main thread!
}
}];

关于ios - 如何从 GCD 返回 UIImage 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18328379/

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