Get UIImageView using Afnetworking and put it in an array

旧巷老猫 提交于 2019-12-06 16:46:07

问题


As the title says, i need to get the image and put it in an array.

UIImageView *myImage = [[UIImageView alloc] init];
[myImage setImageWithURL:[NSURL URLWithString:@"http://host.com/images/1/1.png"] placeholderImage:[UIImage imageNamed:@"page3.png"]];
[items addObject:myImage]; 

the problem is that while the image is being loaded, the code continues and nothing gets put in the array, actually I guess an empty UIImageView gets inserted.

How do I go about fixing this?

On a side note I am using iCarousel to display images, in the data source it gets the images array and shows them. Can i modify iCarousel somehow?

Thanks in advance.


回答1:


You can use the setImageWithURLRequest method to give a success message or execute the UI update when it has finished for example using the following method:

- (void)setImageWithURLRequest:(NSURLRequest *)urlRequest 
          placeholderImage:(UIImage *)placeholderImage 
                   success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success
                   failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure;

Here is an example (not tested)

[image setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"host.com/images/1/1.png"]] placeholderImage:[UIImage imageNamed:@"page3.png"] 
                   success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){ 
                             //reload data here
                   }
                   failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
                             //handle errors here
                   }


来源:https://stackoverflow.com/questions/10290810/get-uiimageview-using-afnetworking-and-put-it-in-an-array

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