提问人:user3092286 提问时间:12/12/2013 最后编辑:Chitra Khatriuser3092286 更新时间:12/19/2013 访问量:117
如何在iPhone中将图像从url放置到Imageview
How to place image from url to Imageview in iPhone
问:
在我的应用程序中,我正在从 url 下载图像并将其显示在 uitableview 单元格的 uiimageview 中。
在这里,我遇到了我正在使用的问题是我正在使用,但是在移动设备中,由于缓存已满,它崩溃了。所以我没有得到任何解决方案来解决这个问题。Lazyloading
如果我以异步方式下载,每当我滚动表格视图时,它都会为 UItableView 中的每个单元格下载,因此下载它需要很长时间,因为我从服务器获得的图像非常大并且分辨率非常高,所以需要更多时间。
请帮助我,我正在尝试的任何一个,但我没有得到任何解决方案。我对 iphone 开发非常陌生。请有人给出任何建议,我也在延迟加载中使用了 SdWebImage,所以我没有得到任何解决我的问题的方法。
谢谢&问候 斯拉维亚
答:
您必须从自己的角度管理您的兑现。 如果你不想做很多事情,请使用AFnetworking Library
使用 UIImageView 类别:UIImageView 类别
[imageViewObject setImageWithURL:YOUR_URL_FORIMAGE_HERE];
我希望这对您有所帮助,而且很容易。
您必须先调整图像大小,然后才能缓存它们/在图像视图上显示它们。 无论使用哪种图像格式,当图像显示在视图中时,它都会转换为位图,并且图像越大,内存占用量就越大。查看此处的代码,了解一些 UIImage 调整大小类别,这些类别应该可以帮助您实现这一目标。
使用此链接:
https://github.com/nicklockwood/AsyncImageView
下载代码:
- 拖放 和 在您的项目中
AsyncImageView.h
AsyncImageView.m
- 使用 UITableView 委托方法
下面是示例代码:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [imageURLs count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
#define IMAGE_VIEW_TAG 99
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
//create new cell
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
//add AsyncImageView to cell
AsyncImageView *imageView = [[AsyncImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 44.0f, 44.0f)];
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES;
imageView.tag = IMAGE_VIEW_TAG;
[cell addSubview:imageView];
[imageView release];
//common settings
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
cell.indentationWidth = 44.0f;
cell.indentationLevel = 1;
}
//get image view
AsyncImageView *imageView = (AsyncImageView *)[cell viewWithTag:IMAGE_VIEW_TAG];
//cancel loading previous image for cell
[[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:imageView];
//load the image
imageView.imageURL = [imageURLs objectAtIndex:indexPath.row];
//display image path
cell.textLabel.text = [[[imageURLs objectAtIndex:indexPath.row] path] lastPathComponent];
return cell;
}
如果在 AsyncImageView 的拖放类中遇到任何错误,请单击 prject->Build Phases->Compile Sources。
在 AsyncImageView.m 中写入 -fno-objc-arc
试试吧。希望能帮助你
评论