objective-c – 使用来自异步NSURLConnection的数据填充NSImage

objective-c – 使用来自异步NSURLConnection的数据填充NSImage,第1张

概述我已经碰到了众所周知的问题,试图弄清楚如何使用桌面应用程序中的异步NSURLConnection返回的数据填充NS Image(不是iPhone应用程序!!). 情况就是这样. 我有一个使用自定义单元格的表.在每个自定义单元格中都是从Web服务器中提取的NSImage.为了填充图像,我可以轻松地执行同步请求: myThumbnail = [[NSImage alloc] initWithCont 我已经碰到了众所周知的问题,试图弄清楚如何使用桌面应用程序中的异步NSURLConnection返回的数据来填充NS Image(不是iPhone应用程序!!).

情况就是这样.

我有一个使用自定义单元格的表.在每个自定义单元格中都是从Web服务器中提取的NSImage.为了填充图像,我可以轻松地执行同步请求:

mythumbnail = [[NSImage alloc] initWithContentsOffile:myfilePath];

这个问题是表格会阻塞,直到填充图像(显然是因为它是同步请求).在一张大桌子上,这使得滚动难以忍受,但即使只是在第一次运行时填充图像,如果它们具有任何显着大小,也可能是乏味的.

所以我创建了一个异步请求类,它将按照Apple’s documentation在自己的线程中检索数据.没问题.我可以看到数据被拉动和填充(通过我的日志文件).

我遇到的问题是,一旦我有数据,我需要回调到我的调用类(自定义表视图).

我的印象是我可以做这样的事情,但它不起作用,因为(我假设)我的调用类真正需要的是一个委托:

NSImage * myIMage;myImage = [myConnectionClass getMyImageMethod];

在我的连接类委托中,我可以看到我获取数据,我只是没有看到如何将它传递回调用类.我的connectionDIDFinishLoading方法直接来自Apple文档:

- (voID)connectionDIDFinishLoading:(NSURLConnection *)connection{    // do something with the data    // receivedData is declared as a method instance elsewhere    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);    // release the connection,and the data object    [connection release];    [receivedData release];}

我希望这是一个简单的问题需要解决,但我担心我对这个问题的知识极限,尽管有一些严肃的谷歌搜索并尝试了许多不同的推荐方法,但我仍在努力想出一个解决方案.

最终,我将为我的应用程序提供一个复杂的缓存机制,其中表格视图在外出之前检查本地计算机上的图像并从服务器获取它们,并且可能有一个进度指示器,直到检索到图像.现在,如果图像使用同步过程足够大,那么即使是局部图像填充也会很慢.

任何和所有的帮助将非常感谢.

解决方案更新

如果其他人需要一个类似的解决方案,感谢Ben的帮助,这就是我提出的(通常修改为发布当然).请记住,我还实现了图像的自定义缓存,并使我的图像加载类足够通用,以供我的应用程序中的各个地方用于调用图像.

在我的调用方法中,在我的情况下是表中的自定义单元格…

ImageLoaderClass * myLoader = [[[ImageLoaderClass alloc] init] autorelease];    [myLoader fetchImageWithURL:@"/my/thumbnail/path/with/filename.png"                     forMethod:@"myUniqueRef"                        withID:1234                   savetoCache:YES                     cachePath:@"/path/to/my/custom/cache"];

这将创建myLoader类的实例并将其传递给4个参数.我想要获取的图像的URL,我用来确定在设置通知观察者时调用哪个类的唯一引用,图像的ID,我是否要将图像保存到缓存中以及路径到缓存.

我的ImageLoaderClass定义了上面调用的方法,我在其中设置从调用单元格传递的内容:

-(voID)fetchImageWithURL:(Nsstring *)imageURL            forMethod:(Nsstring *)methodPassed               withID:(int)imageIDPassed          savetoCache:(BOol)shouldisaveThis           cachePath:(Nsstring *)cachePathToUse{    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:imageURL]                                          cachePolicy:NSURLRequestUseProtocolCachePolicy                                      timeoutInterval:60.0];    // Create the connection with the request and start loading the data    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];    if (theConnection) {        // Create the NSMutableData that will hold        // the received data         // receivedData is declared as a method instance elsewhere        receivedData = [[NSMutableData data] retain];        // Now set the variables from the calling class        [self setCallingMethod:methodPassed];        [self setimageID:imageIDPassed];        [self setSaveImage:shouldisaveThis];        [self setimageCachePath:cachePathToUse];    } else {        // Do something to tell the user the image Could not be downloaded    }}

在connectionDIDFinishLoading方法中,我根据需要将文件保存到缓存中,并向任何监听观察者发出通知调用:

- (voID)connectionDIDFinishLoading:(NSURLConnection *)connection{    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);    // Create an image representation to use if not saving to cache    // And create a dictionary to send with the notification        NSImage * mImage = [[NSImage alloc ] initWithData:receivedData];    NSMutableDictionary * mDict = [[NSMutableDictionary alloc] init];    // Add the ID into the dictionary so we can reference it if needed    [mDict setobject:[NSNumber numberWithInteger:imageID] forKey:@"imageID"];    if (saveImage)    {        // We just need to add the image to the dictionary and return it        // because we aren't saving it to the custom cache        // Put the mutable data into NSData so we can write it out        NSData * dataToSave = [[NSData alloc] initWithData:receivedData];        if (![dataToSave writetofile:imageCachePath atomically:NO])    NSLog(@"An error occured writing out the file");    }    else    {        // Save the image to the custom cache        [mDict setobject:mImage forKey:@"image"];    }    // Now send the notification with the dictionary    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];    [nc postNotificationname:callingMethod object:self userInfo:mDict];    // And do some memory management cleanup    [mImage release];    [mDict release];    [connection release];    [receivedData release];}

最后在表控制器中设置一个观察者来监听通知并将其发送给方法来处理重新显示自定义单元格:

-(ID)init{    [super init];    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];    [nc addobserver:self selector:@selector(updateCellData:) name:@"myUniqueRef" object:nil];    return self;}

问题解决了!

解决方法 你的直觉是正确的;您希望从对象获得回调,该对象是NSURLConnection对管理表视图的控制器的委托,该控制器将更新数据源,然后使用图像对应的行的rect调用-setNeedsdisplayInRect:. 总结

以上是内存溢出为你收集整理的objective-c – 使用来自异步NSURLConnection的数据填充NSImage全部内容,希望文章能够帮你解决objective-c – 使用来自异步NSURLConnection的数据填充NSImage所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1011884.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存