gpt4 book ai didi

ios - 使用图像选择器将图像加载到两个 Collection View 单元格

转载 作者:行者123 更新时间:2023-11-30 13:05:30 25 4
gpt4 key购买 nike

我目前正在构建一个应用程序,其目标是用户可以通过点击单元格内的按钮将图像上传到所需的 Collection View 单元格。到目前为止,我已经能够选择图像,但它永远不会进入指定的单元格。为了简化问题,我将显示前三个单元格使用的代码。

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let returnCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell1", forIndexPath: indexPath) as! FirstCollectionViewCell

if indexPath.row == 0 {
let cell1 = collectionView.dequeueReusableCellWithReuseIdentifier("cell1", forIndexPath: indexPath) as! FirstCollectionViewCell
cell1.backgroundColor = UIColor.redColor()
cell1.firstAdd.addTarget(self, action: "importPicture:", forControlEvents: UIControlEvents.TouchUpInside)
currentPickerTarget = cell1.firstImages
return cell1

} else if indexPath.row == 1 {

let cell2 = collectionView.dequeueReusableCellWithReuseIdentifier("cell2", forIndexPath: indexPath) as! SecondCollectionViewCell
cell2.backgroundColor = UIColor.blueColor()
cell2.secondAdd.addTarget(self, action: "secondImport:", forControlEvents: UIControlEvents.TouchUpInside)
secondPickerTarget = cell2.secondImages
return cell2

} else if indexPath.row == 2 {
let cell3 = collectionView.dequeueReusableCellWithReuseIdentifier("cell3", forIndexPath: indexPath) as! ThirdCollectionViewCell
cell3.backgroundColor = UIColor.redColor()
cell3.thirdAdd.addTarget(self, action: #selector(ViewController.importPicture(_:)), forControlEvents: .TouchUpInside)
return cell3
}
return returnCell

}

func importPicture(sender: UIButton) {

let point = myCollectionView.convertPoint(CGPoint.zero, fromView: sender)
let indexPath = myCollectionView.indexPathForItemAtPoint(point)
let cell1 = myCollectionView.dequeueReusableCellWithReuseIdentifier("cell1", forIndexPath: indexPath!) as! FirstCollectionViewCell

currentPickerTarget = cell1.firstImages

imagePicker.delegate = self
imagePicker.sourceType = .PhotoLibrary
imagePicker.allowsEditing = true

presentViewController(imagePicker, animated: true, completion: nil)
}

func secondImport(sender: UIButton) {
let point = myCollectionView.convertPoint(CGPoint.zero, fromView: sender)
let indexPath = myCollectionView.indexPathForItemAtPoint(point)
let cell2 = myCollectionView.dequeueReusableCellWithReuseIdentifier("cell2", forIndexPath: indexPath!) as! SecondCollectionViewCell

print("\(indexPath!.row)")

secondPickerTarget = cell2.secondImages
secondPicker.delegate = self
secondPicker.sourceType = .PhotoLibrary
secondPicker.allowsEditing = true

presentViewController(secondPicker, animated: true, completion: nil)

}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

imagePicker.dismissViewControllerAnimated(true, completion: nil)
secondPicker.dismissViewControllerAnimated(true, completion: nil)

let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
currentPickerTarget.image = pickedImage
secondPickerTarget.image = pickedImage
}

最佳答案

您可以制作一组默认图像或空白图像,并在 ImageView 上方的每个单元格上添加一个按钮。根据单元格将标签分配给按钮。当用户点击任何单元格时,检测其计数,然后从图库或相机中选择图片,更新数组中索引处的对象并重新加载 Collection View ,或者您可以在 Collection View 中重新加载特定单元格。希望这有帮助:)

关于ios - 使用图像选择器将图像加载到两个 Collection View 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39425299/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com