Load images from NSURL async with RestKit

眉间皱痕 提交于 2019-12-04 13:18:23

问题


Is there a wrapper or some sort of built-in functionality available in RestKit to load a UIImage from an NSURL asynchronously using callbacks or blocks? I could not find such a method in the RestKit docs. If there is not, what is a good strategy for implementing lazy loaded async images from NSURL using RestKit as much as possible?


回答1:


Using RestKit you can use RKRequest to load the data for the image in a manner such as:

RKRequest* request = [RKRequest requestWithURL: url];

request.onDidLoadResponse = ^(RKResponse* response) {
    UIImage* image = [UIImage imageWithData: response.body];
    // do something interesting with the image
};

request.onDidFailLoadWithError = ^(NSError* error) {
    // handle failure to load image
}

[imageLoadingQueue addRequest: request];

Note that even in the onDidLoadResponse case you may want to check response to make sure the type of data is what you expected. The image loading queue used above can be created like so:

imageLoadingQueue = [RKRequestQueue requestQueueWithName: @"imageLoadingQueue"];
[imageLoadingQueue start];



回答2:


Not sure about a RestKit solution, but SDWebImage is a library that will let you easily load images asynchronously by adding a category to UIImageView, so all you have to write is this (for example):

[myImageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
               placeholderImage:[UIImage imageNamed:@"placeholder.png"]];


来源:https://stackoverflow.com/questions/9799472/load-images-from-nsurl-async-with-restkit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!