Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue in Using AsyncImageView in icarousel in iOS

I have developed an app which uses Asyncimageview and iCarousel.But my issues is that when i'm trying to load the images from urls only activity indicator loads in each view of my iCarousel and no images are loaded.Here is my code

  - (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(AsyncImageView *)view
 {
if (view == nil
 {
  AsyncImageView * view = [[[AsyncImageView alloc]initWithFrame:CGRectMake(0, 0, 300, 280)] autorelease];
view.image=[UIImage imageNamed:@"infobg.png"];
view.imageURL=[imageURLs objectAtIndex:index];
}
return view;
}
like image 401
jai Avatar asked Oct 31 '12 10:10

jai


3 Answers

Follow these steps maybe because of following reasons you may face this issue

-- Check whether if ur imageURLs array is having objects under iCarousel are not...If its null you may have this kind of issue....

-- Do array allocation and add objects in

 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 

method,because if you add objects in your array under viewDidLoad it won't works because carousel view loads first before viewDidLoad method..

-- if your array element is present even after these steps follow this code.

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(AsyncImageView *)view
{
view = [[[AsyncImageView alloc]initWithFrame:CGRectMake(0, 0, 300, 280)] autorelease];

view.image=[UIImage imageNamed:@"infobg.png"];
view.imageURL=[imageURLs objectAtIndex:index];

//NSLog(@"%@",imageURLs)//check imageURLs having object

if(view ==nil)
{
    [[AsyncImageLoader sharedLoader]cancelLoadingImagesForTarget:view];
}
return view;

}

It will help you..

like image 199
Vishnu Avatar answered Oct 31 '22 14:10

Vishnu


Cells should be reused

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(AsyncImageView *)view

 {
if (view == nil) {
 view = [[[AsyncImageView alloc]initWithFrame:CGRectMake(0, 0, 300, 280)] autorelease];
}
// dont forget stop previous loading -cancelLoadingURL:target:

 view.image=[UIImage imageNamed:@"infobg.png"];
 view.imageURL=[imageURLs objectAtIndex:index];

 return view;
}

also you should stop previously started

like image 2
NeverBe Avatar answered Oct 31 '22 14:10

NeverBe


- (void)viewDidLoad
{
    [super viewDidLoad];

    User_Id=@"[email protected]";

    NSString *Post=[NSString stringWithFormat:@"email=%@",User_Id];
    NSData *PostData = [Post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
    NSString *PostLengh=[NSString stringWithFormat:@"%d",[Post length]];
    NSURL *Url=[NSURL URLWithString:[NSString stringWithFormat:@"%@fetch_all_user_updates.php",ServerPath]];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:Url     cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    [request setHTTPMethod:@"POST"];
    [request setValue:PostLengh forHTTPHeaderField:@"Content-Lenght"];
    [request setHTTPBody:PostData];

    NSData *ReturnData =[NSURLConnection sendSynchronousRequest:request returningResponse:Nil error:Nil];
    NSString *Response = [[NSString alloc] initWithData:ReturnData encoding:NSUTF8StringEncoding];
    Response = [Response stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSMutableArray *JSON_Array=[Response JSONValue];

// NSlog(@"%@", JSON_Array);
// textfield.text=[[JSON_Array valueforKey:@"email"]objectAtIndex:0];
// load images from database at local host
/*
NSLog(@"%@",[NSString stringWithFormat:@"%@/Images/%@",serverScriptpath,[[jsonarray valueForKey:@"image"]objectAtIndex:0]]);
NSURL *img_url=[NSURL URLWithString:[NSString stringWithFormat:@"%@/Images/%@",
                                     serverScriptpath,[[jsonarray valueForKey:@"image"]objectAtIndex:0]]];

NSURLRequest *request1=[NSURLRequest requestWithURL:img_url];
[Img_profilepic setImageWithURLRequest:request1 placeholderImage:[UIImage imageNamed:@".png"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {

} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {

}];
*/


    if (JSON_Array>0)
    {
        Array_Image_Name=[JSON_Array valueForKey:@"image_name"];
        [Array_Image_Name retain];
    }
    else
    {
        UIAlertView *Alert=[[UIAlertView alloc]initWithTitle:@"Failure" message:@"Error To Load Image" delegate:Nil cancelButtonTitle:@"OK" otherButtonTitles:Nil, nil];
        [Alert show];
        [Alert release];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return Array_Image_Name.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell;
    cell=nil;
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    AsyncImageView *Load_Image=[[AsyncImageView alloc]initWithFrame:CGRectMake(20, 10, 280, 100)];
    Load_Image.imageURL=[NSURL URLWithString:[NSString stringWithFormat:@"%@/Images/%@",ServerPath,[Array_Image_Name objectAtIndex:indexPath.row]]];
    Load_Image.showActivityIndicator=YES;
    [cell.contentView addSubview:Load_Image];
    return cell;
}
like image 1
user2704002 Avatar answered Oct 31 '22 14:10

user2704002