目前我有collectionview
个单元格,可以从library
加载图片。我希望在此之前添加一个带按钮的单元格来打开camera
。我该怎么做??
import UIKit
import Photos
import MobileCoreServices
private let reuseIdentifier = "PhotoCell"
class AddPhotoViewController: UIViewController , UIImagePickerControllerDelegate ,UINavigationControllerDelegate ,UICollectionViewDataSource ,UICollectionViewDelegate{
@IBOutlet weak var photoAlbum: UICollectionView!
var TakenImage : UIImageView!
var selectedImage : UIImage!
var pickedImage : UIImage!
var assetCollection: PHAssetCollection!
var photosAsset: PHFetchResult!
var assetThumbnailSize: CGSize!
let imagePicker: UIImagePickerController! = UIImagePickerController()
var cameraon : Bool = false
var index : [NSIndexPath]!
var note : String!
var noteAlreadyEntered = false
override func viewDidLoad()
{
super.viewDidLoad()
let collection:PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumUserLibrary, options: nil)
var i = 0
repeat
{
if let first_Obj:AnyObject = collection.objectAtIndex(i)
{
self.assetCollection = first_Obj as! PHAssetCollection
}
i++
}while( i < collection.count)
// Do any additional setup after loading the view.
}
@IBAction func takePhoto(sender: AnyObject) {
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .Camera
presentViewController(picker, animated: true,completion : nil)
save(TakenImage.image!)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
TakenImage.image = info[UIImagePickerControllerOriginalImage] as? UIImage ; dismissViewControllerAnimated(true, completion: nil )
}
@IBAction func save(sender: AnyObject)
{
UIImageWriteToSavedPhotosAlbum(TakenImage.image!, self, "image:didFinishSavingWithError:contextInfo:", nil)
}
func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>)
{
if error == nil {
let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .Alert)
ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
presentViewController(ac, animated: true, completion: nil)
} else
{
let ac = UIAlertController(title: "Save error", message: error?.localizedDescription, preferredStyle: .Alert)
ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
presentViewController(ac, animated: true, completion: nil)
}
}
override func viewWillAppear(animated: Bool)
{
if let layout = self.photoAlbum!.collectionViewLayout as? UICollectionViewFlowLayout{
let cellSize = layout.itemSize
self.assetThumbnailSize = CGSizeMake(cellSize.width, cellSize.height)
}
//fetch the photos from collection
self.photosAsset = PHAsset.fetchAssetsInAssetCollection(self.assetCollection, options: nil)
self.photoAlbum!.reloadData()
}
// MARK: UICollectionViewDelegate
/*
// Uncomment this method to specify if the specified item should be selected
override func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
*/
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if (segue.identifier == "savePhoto")
{
if let controller : NoteDetailViewController = segue.destinationViewController as? NoteDetailViewController
{
controller.takinPhoto = true
// controller.imageView2.image = TakenImage.image
if(noteAlreadyEntered == true)
{
controller.content = note
controller.imageView.image = TakenImage.image
}
else
{
controller.imageView2.image = TakenImage.image
}
}
}
if (segue.identifier == "saveSelected")
{
let cell = sender as! PhotoAlbumCollectionViewCell
let indexPath = photoAlbum.indexPathForCell(cell)
let destVC = segue.destinationViewController as! NoteDetailViewController
destVC.asset = self.photosAsset[indexPath!.item] as! PHAsset
destVC.flag = true
if(noteAlreadyEntered == true)
{
destVC.content = note
}
}
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
{
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int
{
// #warning Incomplete implementation, return the number of sections
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
// #warning Incomplete implementation, return the number of items
var count: Int = 0
if(self.photosAsset != nil){
count = self.photosAsset.count
}
print("\(self.photosAsset.count)")
return count + 1
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell: PhotoAlbumCollectionViewCell = photoAlbum.dequeueReusableCellWithReuseIdentifier("PhotoCell", forIndexPath: indexPath) as! PhotoAlbumCollectionViewCell
//Modify the cell
let asset: PHAsset = self.photosAsset[indexPath.item] as! PHAsset
PHImageManager.defaultManager().requestImageForAsset(asset, targetSize: self.assetThumbnailSize, contentMode: .AspectFill, options: nil, resultHandler: {(result, info)in
if let image = result {
cell.setThumbnailImage(image)
}
})
return cell
}
func collectionView(collectinView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
return 4
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
return 1
}
func collectionView(collectionView: UICollectionView, shouldShowMenuForItemAtIndexPath indexPath: NSIndexPath) -> Bool {
return false
}
func collectionView(collectionView: UICollectionView, canPerformAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) -> Bool {
return false
}
func collectionView(collectionView: UICollectionView, performAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) {
self.dismissViewControllerAnimated(false, completion: nil)
}
}
答案 0 :(得分:1)
您已经在numberOfItemInSection
方法中传递了一个计数。所以只需像这样更改cellForItemAtIndexPath
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell: PhotoAlbumCollectionViewCell = photoAlbum.dequeueReusableCellWithReuseIdentifier("PhotoCell", forIndexPath: indexPath) as! PhotoAlbumCollectionViewCell
if (indexPath.item == 0) {
let btn = UIButton(frame: cell.contentView.bounds) //Set your frame that you want
btn.setBackgroundImage(UIImage(named: "add"), forState: .Normal)
btn.addTarget(self, action: #Selector(addNewImage(_:)), forControlEvents: .TouchUpInside)
cell.contentView.addSubview(btn)
}
else {
//Modify the cell
let asset: PHAsset = self.photosAsset[indexPath.item-1] as! PHAsset
PHImageManager.defaultManager().requestImageForAsset(asset, targetSize: self.assetThumbnailSize, contentMode: .AspectFill, options: nil, resultHandler: {(result, info)in
if let image = result {
cell.setThumbnailImage(image)
}
})
}
return cell
}
现在只需在addNewImage
中添加此ViewController
方法,就像这样
func addNewImage(sender: UIButton) {
//Add the code of ImagePicker
}
要解决segue
问题,您需要覆盖shouldPerformSegueWithIdentifier
ViewController
方法
override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
if (identifier == "saveSelected")
{
let cell = sender as! PhotoAlbumCollectionViewCell
let indexPath = tableView.indexPathForCell(cell)
if (indexPath.row == 0) {
self.addNewImage(UIButton()) // To Open Camera
return false //To stop perform Segue.
}
}
return true
}
答案 1 :(得分:1)
我假设您正在为collectionview单元格使用自定义类,并且已经将相机图像添加到阵列的第一个索引,因为您要添加选项以在图像之前打开相机。这是执行此操作的简单方法。
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Identifier", forIndexPath: indexPath) as! YourCustomcell
cell.imageView.image = UIImage(named: arryImage[indexPath.item] as String)
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
if indexPath.item == 0 {
//Code to open camera
}else{
}
}
这是实现您想要的最简单方法。如果您不想将相机图像添加到图像数组中,那么只需在numberOfItemsInSection方法中将数组计数增加1,然后在cellForItemAtIndexPath方法中进行以下更改
if indexPath.item == 0 {
cell.imageView.image = UIImage(named: "CameraImage")
}else{
cell.imageView.image = UIImage(named: arryImage[(indexPath.item)-1] as String)
}
didSelectItemAtIndexPath方法将保持不变。如果你想在最后使用opencamera函数,那么就进行相应的更改。
希望这会对你有所帮助:)。