Ios 照片框架。如何从所选图像获取日期和位置
我有一个从图书馆加载的collectionview。选定的图像将显示在另一个viewcontroller中。我想从图像中获取exif数据(位置和日期)。我应该如何修改代码来实现这一点 列出图像的页面代码如下所示:Ios 照片框架。如何从所选图像获取日期和位置,ios,swift,photos,Ios,Swift,Photos,我有一个从图书馆加载的collectionview。选定的图像将显示在另一个viewcontroller中。我想从图像中获取exif数据(位置和日期)。我应该如何修改代码来实现这一点 列出图像的页面代码如下所示: import UIKit import Photos import MobileCoreServices private let reuseIdentifier = "PhotoCell" class AddPhotoViewController: UIViewController ,
import UIKit
import Photos
import MobileCoreServices
private let reuseIdentifier = "PhotoCell"
class AddPhotoViewController: UIViewController , UIImagePickerControllerDelegate ,UINavigationControllerDelegate ,UICollectionViewDataSource ,UICollectionViewDelegate{
@IBOutlet weak var photoAlbum: UICollectionView!
var TakenImage : UIImage?
var newMedia: Bool?
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 tags = ""
var noteAlreadyEntered = false
override func viewDidLoad()
{
super.viewDidLoad()
self.navigationController?.navigationBar.barTintColor = UIColor.grayColor()
let collection:PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumUserLibrary, options: nil)
var i = 0
repeat
{
if (collection.count > 0)
{
if let first_Obj:AnyObject = collection.objectAtIndex(i)
{
self.assetCollection = first_Obj as! PHAssetCollection
}
i += 1
}
}while( i < collection.count)
// Do any additional setup after loading the view.
}
func takePhoto(sender : UIButton)
{
if (UIImagePickerController.isSourceTypeAvailable(.Camera))
{
if UIImagePickerController.availableCaptureModesForCameraDevice(.Rear) != nil {
imagePicker.allowsEditing = false
imagePicker.sourceType = .Camera
imagePicker.cameraCaptureMode = .Photo
presentViewController(imagePicker, animated: true, completion: {})
} else {
print("Rear camera doesn't exist Application cannot access the camera.")
}
} else {
print("Camera inaccessable Application cannot access the camera.")
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
print("Got an image")
if let pickedImage:UIImage = (info[UIImagePickerControllerOriginalImage]) as? UIImage {
let selectorToCall = Selector("imageWasSavedSuccessfully:didFinishSavingWithError:context:")
UIImageWriteToSavedPhotosAlbum(pickedImage, self, selectorToCall, nil)
TakenImage = pickedImage
}
imagePicker.dismissViewControllerAnimated(true, completion: {
// Anything you want to happen when the user saves an image
})
}
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 == "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
if (self.tags != "")
{
destVC.tagsTextField.text = self.tags
}
}
}
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
{
if (indexPath.row == 0)
{
let controller = self.storyboard!.instantiateViewControllerWithIdentifier("NoteDetailViewController") as! NoteDetailViewController
controller.takinPhoto = true
if(noteAlreadyEntered == true)
{
controller.content = note
controller.imageView.image = TakenImage
controller.tagsTextField.text = self.tags
}
else
{
controller.imageView2.image = TakenImage
controller.tagsTextField.text = self.tags
}
self.navigationController!.pushViewController(controller, animated: true)
}
}
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
if (indexPath.item == 0)
{
let btn = UIButton(frame: cell.contentView.bounds) //Set your frame that you want
// btn.setBackgroundImage(UIImage(named: "Compact Camera Filled-50.png"), forState: .Normal)
btn.setImage(UIImage(named: "Compact Camera Filled-50.png"), forState: .Normal)
btn.addTarget(self, action: "takePhoto:", forControlEvents: UIControlEvents.TouchUpInside)
cell.contentView.addSubview(btn)
}
else
{
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)
}
}
导入UIKit
导入照片
进口流动储备
private let reuseIdentifier=“光电管”
类AddPhotoViewController:UIViewController、UIImagePickerController Delegate、UINavigationController Delegate、UICollectionViewDataSource、UICollectionViewDelegate{
@IBVAR相册:UICollectionView!
var TakenImage:UIImage?
新媒体:布尔?
var selectedImage:UIImage!
var pickedImage:UIImage!
var assetCollection:PhaseSetCollection!
var photosAsset:PHFetchResult!
var assetThumbnailSize:CGSize!
让ImagePickerController:UIImagePickerController!=UIImagePickerController()
变量cameraon:Bool=false
变量索引:[nsindepath]!
注意:字符串!
var tags=“”
var noteAlreadyEntered=false
重写func viewDidLoad()
{
super.viewDidLoad()
self.navigationController?.navigationBar.barTintColor=UIColor.grayColor()
let collection:PHFetchResult=PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum,子类型:.SmartAlbumUserLibrary,选项:nil)
变量i=0
重复
{
如果(collection.count>0)
{
如果let first_Obj:AnyObject=collection.objectAtIndex(i)
{
self.assetCollection=第一个对象作为!阶段集合
}
i+=1
}
}而(iBool{
返回真值
}
*/
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
覆盖函数prepareforsgue(segue:UIStoryboardSegue,sender:AnyObject?)
{
如果(segue.identifier==“saveSelected”)
{
让单元格=发送者为!PhotoAlbumCollectionViewCell
设indexPath=photoAlbum.indexPathForCell(单元格)
将destVC=segue.destinationViewController设为!NoteDetailViewController
destVC.asset=self.photosAsset[indexPath!.item]as!PHAsset
destVC.flag=true
如果(noteAlreadyEntered==true)
{
destVC.content=注释
如果(self.tags!=“”)
{
destVC.tagsTextField.text=self.tags
}
}
}
}
func collectionView(collectionView:UICollectionView,didSelectItemAtIndexPath indexPath:NSIndexPath)
{
if(indexPath.row==0)
{
让controller=self.storyboard!。将eviewcontrollerwhiteIdentifier(“NoteDetailViewController”)实例化为!NoteDetailViewController
controller.takinPhoto=true
如果(noteAlreadyEntered==true)
{
controller.content=注释
controller.imageView.image=TakenImage
controller.tagsTextField.text=self.tags
}
其他的
{
controller.imageView2.image=TakenImage
controller.tagsTextField.text=self.tags
}
self.navigationController!.pushViewController(控制器,动画:true)
}
}
func numberOfSectionsInCollectionView(collectionView:UICollectionView)->Int
{
//#警告一