我有collectionview
使用照片框架从照片库加载图片。我添加了一个名为saveSelected
的segue,目的是将所选图像传递给另一个viewController
NoteDetailViewController.How我可以通过相同的
我的AddPhotoViewController在下面给出
import UIKit
import Photos
private let reuseIdentifier = "PhotoCell"
class AddPhotoViewController: UIViewController , UIImagePickerControllerDelegate ,UINavigationControllerDelegate ,UICollectionViewDataSource ,UICollectionViewDelegate
{
@IBOutlet weak var photoAlbum: UICollectionView!
var TakenImage : UIImageView!
var assetCollection: PHAssetCollection!
var photosAsset: PHFetchResult!
var assetThumbnailSize: CGSize!
let imagePicker: UIImagePickerController! = UIImagePickerController()
var cameraon : Bool = 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) {
if (UIImagePickerController.isSourceTypeAvailable(.Camera)) {
if UIImagePickerController.availableCaptureModesForCameraDevice(.Rear) != nil {
imagePicker.allowsEditing = false
imagePicker.sourceType = .Camera
imagePicker.cameraCaptureMode = .Photo
presentViewController(imagePicker, animated: true, completion: {})
cameraon = true
} else {
print("Rear camera doesn't exist")
}
} else {
print("Camera inaccessable")
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
TakenImage.image = image
if (cameraon)
{
let imageData = UIImageJPEGRepresentation(TakenImage.image!, 0.6)
let compressedJPGImage = UIImage(data: imageData!)
UIImageWriteToSavedPhotosAlbum(compressedJPGImage!, nil, nil, nil)
}
self.dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
print("User canceled image")
dismissViewControllerAnimated(true, completion: {
// Anything you want to happen when the user selects cancel
})
}
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.imageView.image = TakenImage.image
}
}
/* if (segue.identifier == "saveSelected")
{
if let controller2 : NoteDetailViewController = segue.destinationViewController as? NoteDetailViewController
{
if let cell = photoAlbum.cel as? PhotoAlbumCollectionViewCell
{
}
}
}*/
}
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
}
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(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
}
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)
}
}
我应该如何修改didSelectItemAtIndexPath
和prepareForSegue来实现这一目标?
答案 0 :(得分:1)
您已将segue
设置为CollectionViewCell
,因此无需在didSelectItemAtIndexPath
方法中编写任何内容,请在prepareForSegue
方法中更改此代码
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "saveSelected")
{
let cell = sender as! PhotoAlbumCollectionViewCell
let indexPath = tableView.indexPathForCell(cell)
let destVC = segue.destinationViewController as! NoteDetailViewController
destVC.asset = self.photosAsset[indexPath.item] as! PHAsset
}
}
现在在asset
中创建一个名为NoteDetailViewController
的全局对象,就像这样
var asset: PHAsset!
现在在viewDidLoad
NoteDetailViewController
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
PHImageManager.defaultManager().requestImageForAsset(asset, targetSize: self.assetThumbnailSize, contentMode: .AspectFill, options: nil, resultHandler: {(result, info)in
if let image = result {
self.imageView.image = image
}
})
}
中添加如下代码
var map = new Microsoft.Maps.Map(document.getElementById('myMap'), {
credentials: 'Your Bing Maps Key',
mapTypeId: Microsoft.Maps.MapTypeId.aerial
});
map.setView({ labelOverlay: Microsoft.Maps.LabelOverlay.hidden});
希望这会对你有所帮助。
答案 1 :(得分:0)
在加载控制器之前,您无法设置任何图像视图或插座,因此您需要修改您的segue代码,而另一个控制器要有一个图像,以便在被激活时发送。
if (segue.identifier == "savePhoto")
{
if let controller : NoteDetailViewController = segue.destinationViewController as? NoteDetailViewController
{
controller.image = TakenImage.image
}
}
然后修改segued类:
class NoteDetailViewController: UITableViewController {
var image: UIImage!
}