我有一个UICollectionView用于显示我从文件系统加载的图像。然而,滚动似乎有一个非常糟糕的表现,当向上和向下滚动它有点不稳定,并不像它应该顺利 - 与Apple的照片应用程序相比。
详细信息: 用户可以使用相机选择或拍摄照片,然后将照片全部保存到文件系统中。然后将文件URL保存在核心数据数据库中。
在执行segue时,我来到概览页面,在那里我有集合视图。在这里,我通过保存在数据库中的NSURL加载图像,在加载时我调整图像大小 - 从大约900 kb到45 kb。这工作正常,因此集合视图中的图像小于50 kb,在我看来并不算太多,对吧?
以下是代码:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let width = 90
let height = 90
let theSize = CGSize(width: width, height: height)
return theSize
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let myImageCell:ImageCell = myCollectionView.dequeueReusableCellWithReuseIdentifier("imageCell", forIndexPath: indexPath) as ImageCell
var baseString:String = documentsDirectory()
var pathComponents = [baseString, self.pictures[indexPath.row]]
var theImageFile = NSURL.fileURLWithPathComponents(pathComponents)
let theNewImage = self.scaleNewImage(theImageFile)// self.scaleNewImage(readImageFileFromDirectoryNSURL(self.pictures[0]))
myImageCell.imageCell.image = theNewImage
return myImageCell
}
//这是调整图片大小的代码
func scaleNewImage (url:NSURL) -> UIImage {
let src = CGImageSourceCreateWithURL(url, nil)
let scale = UIScreen.mainScreen().scale
let value = (90) * scale // self.view.bounds.width/2
let parameters = [
kCGImageSourceShouldAllowFloat as String : true,
kCGImageSourceCreateThumbnailWithTransform as String : true,
kCGImageSourceCreateThumbnailFromImageAlways as String : true,
kCGImageSourceThumbnailMaxPixelSize as String : value
]
let imageRef = CGImageSourceCreateThumbnailAtIndex(src, 0, parameters)
let image = UIImage(CGImage: imageRef, scale: scale, orientation: .Up)!
//println(image)
//println(image.size)
return image
}
如果有人可以提供帮助,那将会很棒,谢谢你提前
答案 0 :(得分:0)
使用Haneke
var theImageFile = NSURL.fileURLWithPathComponents(pathComponents)
imageCell.hnk_setImageFromURL(theImageFile!)