Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

ios - Multiple async requests at once in objective c

I have am downloading multiple images from a web server at the same time, and I the problem is they are getting mixed up with each other.

dispatch_async(dispatch_get_global_queue(0,0), ^{
    NSData * data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"website.com"]];
    if (data == nil)
        return;
    dispatch_async(dispatch_get_main_queue(), ^{
        cell.imageView.image = [UIImage imageWithData:data];
    });
});

I got this code from: Getting Image from URL Objective C.

How do i fix this?? also is there any way to speed up download speeds?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use "AsyncImageView" class files it will load image synchronously and it shows the activity indicator while image loading

AsyncImageView is the class file in which it will create connection for each call and when image data downloading completed it will return image for imageview. and if image is already in cache then just return image without creating connection.

You can download "AsyncImageView" class files from following link:- https://www.dropbox.com/s/peazwjvky9fsjd7/Archive.zip

in .m file import AsyncImageView Class

  #import "AsyncImageView.h" 

in your tableview cell at indexpath method

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *CellIdentifier = @"SimpleTableCell";
     UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

     UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(X, y, width, height)];

     NSString *imageURL = [NSString stringWithFormat: @"www.xyz.image.png"];
     AsyncImageView *async = [[AsyncImageView alloc]initWithFrame:CGRectMake(0, 0, width, height)];
    [async loadImageFromURL:[NSURL URLWithString:imageURL]];
    [imageView addSubview:async];
    [cell addSubview:imageView];
    return cell;
}

try this your problem will solve.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

[3] html - How to create even cell spacing within a

2.1m questions

2.1m answers

60 comments

56.9k users

...