图片没有显示在我的collectionviewcells中。(Images arent shown in my collectionviewcells. iOS(swift))

编程入门 行业动态 更新时间:2024-10-24 06:24:33
图片没有显示在我的collectionviewcells中。(Images arent shown in my collectionviewcells. iOS(swift))

我得到了一个数组,其中包含我从相机中获得的所有图像。 它们保存在我的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] let imageT : UIImage = images[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

}

I got an array with all the images i got from the camera. They are saved in my coreData. If a user takes a new photo, the photo will be saved in the core data and then i replace the normal array data with the coreData images. So if i go back to the collection view, only the first and the last image is shown. When i print the images[indexpath.item] out i got the images. But they arent shown in the cells. I have a custom class for the imageView in the cell and the imageView has constraints, so the image cant be out of the visible area i think. I look forward to solve this problem with your help. Thank you.

Here is the code from camera catch :

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); }

And here is the code were i create the cell :

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] let imageT : UIImage = images[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

}

最满意答案

您可以尝试更改: cell.imageView.frame = cell.frame to cell.imageView.frame.size = cell.frame.size

Could you try and change: cell.imageView.frame = cell.frame to cell.imageView.frame.size = cell.frame.size

更多推荐

本文发布于:2023-08-05 04:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1427206.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图片   Images   collectionviewcells   arent   iOS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!