I'm new in iOS programming. So I've newbie question. I'm getting started with AFNetworking 2 and that is the task:
I've a request. Its response is the part of the second request. It means that I have to wait untill first request ends. They follow step-by-step. When I get the second response I parse it and save 20 URLs in format http://lalala-xx.jpg. After that I want to load images and put them into UICollectionView, and I want to do it not all in scope but in scheme "downloaded->straight to cell". I save URLs and images in singleton class and get access to them just like
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cell" forIndexPath:indexPath];
cell.imageView.image = [[UserData sharedUser].images objectAtIndex:indexPath.row];
return cell;
}
The chain of methos looks like
- (void)method1
{
NSString *string = [NSString stringWithFormat:firstRequestString];
NSURL *url = [NSURL URLWithString:string];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
// getting needed part for second request
[self method2:(NSString *)part1];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
// show error
}];
[operation start];
}
Second method:
- (void)method2:(NSString *)part1
{
// lalala making secondRequestString
NSString *string = [NSString stringWithFormat:secondRequestString];
NSURL *url = [NSURL URLWithString:string];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSMutableArray *imageURLs = [[NSMutableArray alloc] init];
// getting needed URLs
[self loadAllImages:(NSMutableArray *)imageURLs];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
// show error
}];
[operation start];
}
Last:
- (void)loadAllImages:(NSMutableArray *)imageURLs
{
// ???
}
I'm stuck. What should I do next? I have 20 URLs, but how should I download images and direct them to ViewController to update image in cells?
I suppouse AFNetworkig can provide me some operation queue.
And I dont like my code now. I use this chain, but I want an independent method2 returning imgURLs. So it should look:
User presses button -> method1 -> method2 -> stop. Wait untill user presses button -> download image1 -> show image1 -> download image2 -> show image2 -> and so on -> download imageN -> show imageN -> stop. I'll repeat, I need to store images in Array, I'll use it after that.
Thx u read that.
///////////////////////////////////// UPDATE /////////////////////////////////////
I found solution. But it does not satisfy me completely. Images come randomly. How to make them load in order?
- (void)loadAllImages:(NSMutableArray *)imageURLs
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
for (NSURL *url in imageURLs)
{
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFImageResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
[[UserData sharedUser].images addObject:responseObject];
[[NSNotificationCenter defaultCenter] postNotificationName:#"CollectionViewRealoadData" object:nil];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
// show error
}];
[manager.operationQueue addOperation:operation];
}
}
You need to get the data from the URLs and then create a UIImage object from that data.
You can get the data from the URL using the NSURL methods
for(NSString *imgString in imageURLs){
NSURL *url = [NSURL URLWithString:imgString];
NSData *imgData = [NSData dataWithContentsOfURL:url];
UIImage *img = [UIImage imageWithData:imgData ];
[imageUrls addObject:img];//this mutable array should be initialized early in view controller life cycle (like ViewDidLoad).
}
Once you have your image object you can add it to your array of images that you are using as a datasource for your collection view.
[_collectionView insertItemsAtIndexPaths:#[[NSIndexPath indexPathForItem:[imageUrls count] - 1 inSection:0]]];
//reload your collection view once you add new data
Related
I know that there are a lot of similar questions to this one, but i didn't find any well-explained one yet.
I have a UITableView that gets its content (which is JSON) from a url, i'm using this method for fetching the JSON data:
-(void)getContents
{
NSString *contentStartString = [NSString stringWithFormat:#"%ld",(long)contentStart];
NSString *contentCountString = [NSString stringWithFormat:#"%ld",(long)contentCount];
NSString *contentsUrl = #"http://www.ana.fm/api/index.php?start=";
contentsUrl = [contentsUrl stringByAppendingString:contentStartString];
contentsUrl = [contentsUrl stringByAppendingString:#"&count="];
contentsUrl = [contentsUrl stringByAppendingString:contentCountString];
NSLog(#"%#",contentsUrl);
NSURL *URL = [NSURL URLWithString:contentsUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
//AFNetworking asynchronous url request
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"%#", responseObject);
jsonContents = [responseObject objectForKey:#"contents"];
[self.tableView reloadData];
tableLoadMoreCapability = true;
} failure:nil];
[operation start];
}
This works perfectly, but then when i reach the end of the table which i can detect using this method:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
float endScrolling = scrollView.contentOffset.y + scrollView.frame.size.height;
if (endScrolling >= scrollView.contentSize.height)
{
if(tableLoadMoreCapability == true){
contentStart = contentStart + 20;
[self updateContentsTable];
}
}
}
As you see i want to execute this method updateContentsTable (when reaching the end of the table) which is this one:
- (void)updateContentsTable
{
tableLoadMoreCapability = false;
NSLog(#"load more rows");
NSString *contentStartString = [NSString stringWithFormat:#"%ld",(long)contentStart];
NSString *contentCountString = [NSString stringWithFormat:#"%ld",(long)contentCount];
NSString *contentsUrl = #"http://www.ana.fm/api/index.php?start=";
contentsUrl = [contentsUrl stringByAppendingString:contentStartString];
contentsUrl = [contentsUrl stringByAppendingString:#"&count="];
contentsUrl = [contentsUrl stringByAppendingString:contentCountString];
NSLog(#"%#",contentsUrl);
NSURL *URL = [NSURL URLWithString:contentsUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
//AFNetworking asynchronous url request
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"%#", responseObject);
jsonContents = [responseObject objectForKey:#"contents"];
[self.tableView reloadData];
} failure:nil];
[operation start];
}
Now i'm using [self.tableView reloadData]; so the code works and the table reloaded with the new data, i don't want that, i need the new rows to be inserted below the old ones, i learned from other answers that i have to use [self.tableView insertRowsAtIndexPaths: .....] but i didn't understand how to do that.
Anyone can clarify how to do that ?
Thanks in advance.
I think you are doing more work than you need. Why not tell the table view that the number of rows you have is larger than the number you get with your first call? Then, as soon as the delegate is asked for a cell at a row beyond the ones you have actually loaded (indicating that the user has scrolled past the data you have available), you then get your next batch of data via JSON. Through the delegate methods, you have precise control over what data is shown in the visible cells. I would avoid the scrolling stuff completely.
The only care you need to take is when (and how) you reload the table: you want to avoid reload calling reload calling reload....
Think of the rows of the table as a window sliding over a (virtual) table of all your rows.
I want to know is there way to download images with my code one by one ? Now async
NSString *urlString = link;
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
completion(operation, responseObject, nil);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[RequestAPI sharedInstance].downloadsCount -= 1;
DDLogError(#"FAIL download image: %#",error);
}];
[requestOperation start];
Try this solution
Create a NSObject class for downloading the images. Lets call this DownloadImages. The class has an array of images to be downloaded arrayOfImagesURL.
Create a delegate for the class DownloadImages to make a call back when you finish downloading an image. View how to this in detail here.
Implement AFNetworking as you like, you can also accomplish this by using the iOS 7 API NSURLSession. So in the completion block of your download task, you need to do 2 things:
Continue downloading the next URL in arrayOfImagesURL.
Do a call back with the delegate to the parent controller for processing.
Tell me if there's something need to be more clarify.
I'm trying to figure out a way to download multiple images with AFNewtorking 2.0. I've read a lot of posts here in SO, but can't find the answer I'm looking for, hope you guys can help me.
The problem is that I want to know when all of the downloads finished and if all images where downloaded.
So I have an array with image URL's ant trying to do something like this.
for(NSString *photoUrlString in self.photos){
NSURL *url = [NSURL URLWithString:photoUrlString];
AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:url]];
requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Image error: %#", error);
}];
[requestOperation start];
}
I've found some answers with putting these requests into a queue and setting max concurrent operations to 1. But don't know how that works really.
Any help is appreciated, thanks in advance!
for(Photo *photo in array){
//form the path where you want to save your downloaded image to
NSString *constPath = [photo imageFullPath];
//url of your photo
NSURL *url = [NSURL URLWithString:photo.serverPath];
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:url]];
op.responseSerializer = [AFImageResponseSerializer serializer];
op.outputStream = [NSOutputStream outputStreamToFileAtPath:constPath append:NO];
op.queuePriority = NSOperationQueuePriorityLow;
[op setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead){
}];
op.completionBlock = ^{
//do whatever you want with the downloaded photo, it is stored in the path you create in constPath
};
[requestArray addObject:op];
}
NSArray *batches = [AFURLConnectionOperation batchOfRequestOperations:requestArray progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
} completionBlock:^(NSArray *operations) {
//after all operations are completed this block is called
if (successBlock)
successBlock();
}];
[[NSOperationQueue mainQueue] addOperations:batches waitUntilFinished:NO];
Try this:
// _group, _queue are iVar variable
dispatch_group_t *_group = dispatch_group_create();
dispatch_queue_t *_queue = dispatch_queue_create("com.company.myqueue2", NULL);
// all files download
for(int i = 0 ; i < numberOfFileDownloads; i++){
dispatch_group_async(_group, _queue, ^{
// here is background thread;
// download file
});
}
// all files are download successfully, this method is called
dispatch_group_notify(_group, _queue, ^{
}
Check out +[AFURLConnectionOperation batchOfRequestOperations:progressBlock:completionBlock:]
Although it's not documented, implementation is self-explanatory. Also it allows you to monitor the progress.
You will need to have an array of HTTP operations prior to using this method (this is if you decided to stick to NSURLConnection-based implementation of AFNetworking).
In my app I should download some JSON files, then I store these URL in a plist as you ca see in my code. After I create an 'AFHTTPRequestOperationManager' and I create a loop where I add some operation for the numbers of my 'url_list'.
NSString* plistPath = [[NSBundle mainBundle] pathForResource:#"url_json" ofType:#"plist"];
NSArray *url_list = [NSArray arrayWithContentsOfFile:plistPath];
self.manager = [AFHTTPRequestOperationManager manager];
for (id element in url_list){
NSURL *url = [NSURL URLWithString:element];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
op.responseSerializer = [AFHTTPResponseSerializer serializer];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
[self.manager.operationQueue addOperation:op];
}
Now this code should be fine, but I want to have two information:
what's the way to know the progress value of my 'manager'?, because I want to know the state of all operation in a single progress value
I want to know when an operation finish, because when an operation finish I should pass 'responseObject' to a method that parse this data
Can you help me?
Take a look at AFNetworking batching documentation:
https://github.com/AFNetworking/AFNetworking#batch-of-operations
It gives you an option to assign progress block which is called on single operation completion and on top of that you can assign completion block which will be called when all operations are completed.
If you need you can still assign completion block to single operation to parse responseObjects.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I create block like this:
1) Define your own completion block,
typedef void(^myCompletion)(BOOL);
2) Create a method which takes your completion block as a parameter,
-(void) myMethod:(myCompletion) compblock{
//do stuff
compblock(YES);
}
3)This is how you use it,
[self myMethod:^(BOOL finished) {
if(finished){
NSLog(#"success");
}
}];
How can I send array in block and then get new array from block?
//here I get array of image id's and go in loop for download it all,
NSString *URLString = [NSString stringWithFormat: #"%#", requestString];
NSURL * url = [NSURL URLWithString:URLString];
NSURLRequest * urlRequest = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
userWithImage = [responseObject copy];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Image error: %#", error);
}];
[requestOperation start];
//here I save it to mutable array and send as completion block,
yep, I think it will be better to send 1 image id and return in block 1 image. And in method there I will call the block - make action with photo separately. so, Is it possible to do?
I can do something like this with NSNotifications, but it will be more widely when it can be in blocks..
1) Define your own completion block
typedef void(^myCompletion)(BOOL finished, NSArray *myArray);
2) Create a method which takes your completion block as a parameter,
-(void)myMethod:(myCompletion)compblock {
//do stuff
NSArray *myArray = ...;
compblock(YES, myArray);
}
3)This is how you use it,
[self myMethod:^(BOOL finished, NSArray *myArray) {
if (finished){
NSLog(#"success");
}
}];
If you just want to write a wrapper around the AFNetworking request you could write a method like this:
- (void)downloadImageWithPath:(NSString *)path completion:(void (^)(AFHTTPRequestOperation *operation, UIImage *image, NSError *error))completion __attribute__((nonnull(2)));
{
NSParameterAssert(completion);
NSURL *url = [NSURL URLWithString:path];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
completion(operation, responseObject, nil);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
completion(operation, nil, error);
}];
[requestOperation start];
}
You would invoke this with something like:
[self downloadImageWithPath:#"http://url/to/image.jpg"
completion:^(AFHTTPRequestOperation *operation, UIImage *image, NSError *error) {
if (error) {
// handle error
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
// Update UI with image
});
}];