数组中的图像未显示在UICollectionview中

时间:2016-08-05 17:23:34

标签: ios swift image uicollectionview uicollectionviewcell

我得到了一个包含我从相机中获取的所有图像的数组。它们保存在我的coreData中。如果用户拍摄新照片,照片将保存在核心数据中,然后我用coreData图像替换普通数组数据。因此,如果我返回到集合视图,则仅显示第一个和最后一个图像。当我打印图像[indexpath.item]时,我得到了图像。但他们没有在细胞中显示出来。我在单元格中有一个imageView的自定义类,而imageView有约束,所以图像不能超出我想的可见区域。我期待着在你的帮助下解决这个问题。谢谢。

以下是来自相机捕获的代码:

 func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {




    photoImage.image = image

    print(images)
    addPhotoLabel.removeFromSuperview()
    if(isEdit == false)
    {

        let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
        let entinity = NSEntityDescription.entityForName("Image", inManagedObjectContext: context)

        let newImage = Image(entity: entinity!, insertIntoManagedObjectContext: context)
        newImage.image = UIImageJPEGRepresentation(image, 1)
        do {
            try context.save()
        } catch let error as NSError {
            print(error)
        }
        images.removeAll()
        let request = NSFetchRequest()
        request.entity = entinity
        let fetched = try? context.executeFetchRequest(request)
        if fetched != nil {
            let imagesS = fetched!.map { UIImage(data: ($0 as! Image).image!)}

            images.appendContentsOf(imagesS)

        }




    }
    self.dismissViewControllerAnimated(true, completion: nil);
}

代码是我创建单元格:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
//1

let cell : FlickrPhotoCell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! FlickrPhotoCell





if(searchBarActive == true)
{
    cell.nameLabel.text = researchResults[indexPath.item]
    cell.imageView.image = sFunc_imageFixOrientation(resultImages[indexPath.item])



}
else
{


cell.nameLabel.text = names[indexPath.item]


cell.imageView.image = images[indexPath.item]
print(images[indexPath.item])




}

cell.nameLabel.hidden = false

cell.frame.size = CGSizeMake(UIScreen.mainScreen().bounds.width/2  , UIScreen.mainScreen().bounds.width/2)

cell.imageView.frame = cell.frame



cell.backgroundColor = UIColor.clearColor()
cell.layer.borderColor = themeColor.CGColor
cell.layer.borderWidth = 4
cell.layer.cornerRadius = CGFloat(10.0)
cell.placeHolderName.frame.size = CGSize(width: cell.frame.width, height: cell.frame.height/4)







return cell

}

问我是否需要更多代码。谢谢

1 个答案:

答案 0 :(得分:0)

这个答案可能有所帮助:https://stackoverflow.com/a/14438875/2678994

另外,您可能希望(在故事板中或以编程方式)在UICollectionViewCell中设置UIImageView,然后更新该图像视图的.image值。