gpt4 book ai didi

ios - 访问 DocumentDirectory iOS 和 UICollectionView 中的图像

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

在我的应用程序中,用户从相机胶卷中选择图像,并将其保存在文档目录中。然后图像将显示在他们选择图像的 ViewController 上。我希望将图像附加到 UICollectionView 中。如何从文档目录访问图像/附加图像?请参阅下面我的代码。如果您需要我项目的其他部分,请告诉我。

DetailViewController(我最初显示照片的位置):

class DetailViewController: UIViewController, UITextFieldDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate {

...

var imageStore: ImageStore!

@IBAction func takePicture(sender: UIBarButtonItem) {

let imagePicker = UIImagePickerController()

if UIImagePickerController.isSourceTypeAvailable(.Camera) {
imagePicker.sourceType = .Camera
} else {
imagePicker.sourceType = .PhotoLibrary
}
imagePicker.delegate = self

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

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

let image = info[UIImagePickerControllerOriginalImage] as! UIImage

imageStore.setImage(image, forKey: item.itemKey)

imageView.image = image

dismissViewControllerAnimated(true, completion: nil)
}

override func viewWillAppear(animated: Bool) {

super.viewWillAppear(animated)

let key = item.itemKey

if let imageToDisplay = imageStore.imageForKey(key) {
imageView.image = imageToDisplay
}
}

ImageStore(我最初如何存储照片):

import UIKit

class ImageStore: NSObject {

let cache = NSCache()

func setImage(image: UIImage, forKey key: String) {
cache.setObject(image, forKey: key)

let imageURL = imageURLForKey(key)

if let data = UIImageJPEGRepresentation(image, 0.5) {
data.writeToURL(imageURL, atomically: true)
}
}
func imageForKey(key: String) -> UIImage? {
if let existingImage = cache.objectForKey(key) as? UIImage {
return existingImage
}

let imageURL = imageURLForKey(key)
guard let imageFromDisk = UIImage(contentsOfFile: imageURL.path!) else {
return nil
}

cache.setObject(imageFromDisk, forKey: key)
return imageFromDisk
}

func deleteImageForKey(key: String) {
cache.removeObjectForKey(key)

let imageURL = imageURLForKey(key)
do {
try NSFileManager.defaultManager().removeItemAtURL(imageURL)
}
catch let deleteError {
print("Error removing the image from disk: \(deleteError)")
}
}

func imageURLForKey(key: String) -> NSURL {
let documentsDirectories =
NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
let documentDirectory = documentsDirectories.first!

return documentDirectory.URLByAppendingPathComponent(key)
}

}

UICollectionView:

import UIKit

class PhotosViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

var collectionView: UICollectionView!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
layout.itemSize = CGSize(width: 300, height: 490)

collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
collectionView.dataSource = self
collectionView.delegate = self
collectionView!.registerClass(FoodCell.self, forCellWithReuseIdentifier: "Cell")
collectionView.backgroundColor = UIColor.whiteColor()
self.view.addSubview(collectionView)
}

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}

var images: [UIImage] = [

]

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! FoodCell
cell.textLabel.text = ""
cell.imageView.image = images[indexPath.row]
return cell
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
{
print("User tapped on item \(indexPath.row)")
}


override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

最佳答案

您可以尝试通过以下方式保存并获取您的图片:

//Save image
let img = UIImage() // Image from your picker
let data = UIImagePNGRepresentation(img)!
do {
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
try data.writeToFile("\(documentsPath)myImage", options: [])

} catch {
print("Error")
}

// Get image
do {
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let readData = try NSData(contentsOfFile: "\(documentsPath)myImage", options: [])
let retreivedImage = UIImage(data: readData)
}
catch {
print("Error")
}

https://stackoverflow.com/a/35685943/2894160 相同

关于ios - 访问 DocumentDirectory iOS 和 UICollectionView 中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41063236/

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