我正在尝试从URL获取图像,但它似乎对我不起作用.有人能指出我正确的方向吗?
这是我的代码:
NSURL *url = [NSURL URLWithString:@"http://myurl/mypic.jpg"];
NSString *newIMAGE = [[NSString alloc] initWithContentsOfURL:url
encoding:NSUTF8StringEncoding error:nil];
cell.image = [UIImage imageNamed:newIMAGE];
Run Code Online (Sandbox Code Playgroud)
当我调试newIMAGE字符串是nil所以有些东西不能在那里工作.
我想使用swift在我的表视图中使用延迟加载概念.在我的表视图中,我显示了多个包含产品图像和产品名称的单元格.请帮我解决问题.
我使用异步块(Grand central dispatch)来加载我的单元格图像.但是,如果你快速滚动它们仍然会出现但速度非常快,直到它加载了正确的一个.我确定这是一个常见的问题,但我似乎无法找到它.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Load the image with an GCD block executed in another thread
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[[appDelegate offersFeeds] objectAtIndex:indexPath.row] imageurl]]];
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *offersImage = [UIImage imageWithData:data];
cell.imageView.image = offersImage;
});
});
cell.textLabel.text = [[[appDelegate offersFeeds] objectAtIndex:indexPath.row] title];
cell.detailTextLabel.text = [[[appDelegate offersFeeds] objectAtIndex:indexPath.row] subtitle];
return cell;
}
Run Code Online (Sandbox Code Playgroud) 我正在设计一个使用NSURLSession的应用程序,并考虑将它放在与Grand Central Dispatch不同的线程中,但如果NSURLSession在后台自动执行此操作,那么我不必使用GCD,对吗?
换句话说,NSURLSession会在后台自动使用Grand Central Dispatch,所以我们不必担心吗?
我在这里已经阅读了很多答案,通过使用AlamofireImage辅助方法(例如af_setImageWithURL())或任何等效的库,您不需要担心细胞重用的东西(另外,许多已发布的教程实际上只是这样做).也就是说,cellForRowAtIndexPath()在后台下载完成后,我不必通过使用tableView的方法更新其imageView 来保持对单元格的弱引用或获取它,就像我们通常在手动请求时所做的那样.
首先,这是真的吗?如果是这样,它是如何在库内完成的,因为我试图追踪AlamofireImage的af_setImageWithURL()代码,我找不到任何努力来确保我们仍然在处理我们提出请求的同一个单元格.我错过了什么吗?
我很抱歉,如果这听起来很愚蠢,但我真的很困惑.
注意:请没有库。这对我来说很重要。另外,对此有各种各样的答案,但我发现没有一个能很好地解决该问题。请不要将其标记为重复项。提前致谢!
我的问题是,如果您在表中快速滚动,就会看到旧图像并闪烁。
URLSession
数据请求。但是我不知道如何在正确的时间和地点做到这一点。可能还有其他解决方案,但不确定。这是我到目前为止的内容:
图像缓存类
class Cache {
static let shared = Cache()
private let cache = NSCache<NSString, UIImage>()
var task = URLSessionDataTask()
var session = URLSession.shared
func imageFor(url: URL, completionHandler: @escaping (image: Image? error: Error?) -> Void) {
if let imageInCache = self.cache.object(forKey: url.absoluteString as NSString) {
completionHandler(image: imageInCache, error: nil)
return
}
self.task = self.session.dataTask(with: url) { data, response, error in
if let error = error {
completionHandler(image: nil, error: Error)
return
}
let image = …Run Code Online (Sandbox Code Playgroud) 我有一个tableView在单元格中显示图像。在大多数情况下,将显示正确的图像,但是偶尔会显示错误的图像(通常是非常快速地向下滚动tableView)。我异步下载图像并将其存储在缓存中。找不到导致问题的其他原因吗??下面是代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// try to reuse cell
let cell:CustomCell = tableView.dequeueReusableCellWithIdentifier("DealCell") as CustomCell
// get the venue image
let currentVenueImage = deals[indexPath.row].venueImageID
let unwrappedVenueImage = currentVenueImage
var venueImage = self.venueImageCache[unwrappedVenueImage]
let venueImageUrl = NSURL(string: "http://notrealsite.com/restaurants/\(unwrappedVenueImage)/photo")
// reset reused cell image to placeholder
cell.venueImage.image = UIImage(named: "placeholder venue")
// async image
if venueImage == nil {
let request: NSURLRequest = NSURLRequest(URL: venueImageUrl!)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in …Run Code Online (Sandbox Code Playgroud) ios ×6
uitableview ×5
swift ×4
objective-c ×3
uiimageview ×2
iphone ×1
nscache ×1
nsurlsession ×1