在 iOS 的 icarousel 中使用 AsyncImageView 的问题
Posted
技术标签:
【中文标题】在 iOS 的 icarousel 中使用 AsyncImageView 的问题【英文标题】:Issue in Using AsyncImageView in icarousel in iOS 【发布时间】:2012-10-31 10:30:39 【问题描述】:我开发了一个使用 Asyncimageview 和 iCarousel 的应用程序。但我的问题是,当我尝试从 url 加载图像时,我的 iCarousel 的每个视图中只加载活动指示器并且没有加载任何图像。这是我的代码
- (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;
【问题讨论】:
尝试放置 NSLog(@"%@", [imageURLs objectAtIndex:index]);并检查它不为零。 另外,NeverBe 下面的回答是正确的,即使它没有解决您的问题。您编写的代码将无法正常工作。 【参考方案1】:按照这些步骤可能是由于以下原因您可能会遇到此问题
-- 检查你的 imageURLs 数组是否有 iCarousel 下的对象不是...如果它为 null 你可能有这种问题......
-- 做数组分配和添加对象
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
方法,因为如果您在 viewDidLoad 下的数组中添加对象,它将不起作用,因为轮播视图在 viewDidLoad 方法之前首先加载..
-- 如果您的数组元素即使在按照此代码执行这些步骤之后仍然存在。
- (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;
它会帮助你..
【讨论】:
【参考方案2】:细胞应该重复使用
- (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;
你也应该停止之前启动的
【讨论】:
我仍然有同样的问题,只有指标加载,如果我使用 [AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:view];甚至活动指示器都没有显示...有什么想法吗? @NeverBe 你能在上面的代码中解释如何重用单元格并停止以前的加载方法......我很久以前也有同样的问题,但切换到另一种方法...... @VishnuPrasath 您应该在新图像加载之前使用正确的参数调用 cancelLoadingURL:target @jai 您是否尝试在浏览器中打开这些链接? @NeverBe ya 这些图像在浏览器中运行良好...我使用了 cancelloadingURL:[imageURLs objectAtIndex:index] target:view 这是正确的方法吗?【参考方案3】:- (void)viewDidLoad
[super viewDidLoad];
User_Id=@"abcd@gmail.com";
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;
【讨论】:
【参考方案4】:AsyncImageView *asyncImage = [[AsyncImageView alloc] initWithFrame:CGRectMake(0.0f, 1.0f, 320.0f, 174.0f)];
NSString *imgUrlString = photobig1;
while ([imgUrlString rangeOfString:@" "].location != NSNotFound)
imgUrlString = [imgUrlString stringByReplacingOccurrencesOfString:@" " withString:@""];
UIImageView *imageview = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Loading Image.png"]];
[asyncImage addSubview:imageview];
asyncImage.tag = 999;
NSString *webStr = [NSString stringWithFormat:@"%@",imgUrlString];
NSURL *imageUrl = [[NSURL alloc] initWithString:[webStr stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
[asyncImage loadImageFromURL:imageUrl];
[asyncImage setBackgroundColor:[UIColor clearColor]];
asyncImage.userInteractionEnabled = NO;
[scrollView addSubview:asyncImage];
【讨论】:
以上是关于在 iOS 的 icarousel 中使用 AsyncImageView 的问题的主要内容,如果未能解决你的问题,请参考以下文章