【问题标题】:Creating Array of Collected Images创建收集的图像数组
【发布时间】:2016-11-15 14:39:13
【问题描述】:

我正在创建一组从用户那里收集的图像。该数组用于在 UICollectionView 中显示图像。我正在将图像收集/存储在一个名为 ImageStore.swift 的文件中,请参见下文。另外,请参阅我的 UICollection 视图中的数组。我在数组中放什么来传递图像?

带有 UICollectionView 的视图控制器:

    class PhotosViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
override func viewDidLoad() {
    super.viewDidLoad()
}

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

    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
    layout.itemSize = CGSize(width: 100, height: 100)

    let myCollectionView:UICollectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
    myCollectionView.dataSource = self
    myCollectionView.delegate = self
    myCollectionView.registerClass(RDCellCollectionViewCell.self, forCellWithReuseIdentifier: "MyCell")
    myCollectionView.backgroundColor = UIColor.whiteColor()
    self.view.addSubview(myCollectionView)
}

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

var images: [UIImage] = [

]

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let myCell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCell", forIndexPath: indexPath) as! RDCellCollectionViewCell
    myCell.imageView.image = images[indexPath.item]
    myCell.backgroundColor = UIColor.grayColor()
    return myCell
}

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.
}

}

ImageStore.swift :

    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)
}

}

详细视图控制器:

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)
}

【问题讨论】:

  • 请复制并粘贴您的代码并删除图片。
  • 我复制并粘贴了代码。
  • ^ @shallowThought
  • 我有点困惑。您想知道将数组放在集合视图文件中的什么位置吗?或者您想知道如何将图像放入数组中?
  • 我想知道如何将图像放入数组中。 @CalebKleveter

标签: ios arrays swift uicollectionview photo


【解决方案1】:

大型数据集的最佳方法是使用 SQLite 数据库存储文件名,该文件名可以通过捕获图像的时间戳来区分。 如果您在应用中使用离线功能。

1.Create table for image name
|user_id|image_name|
|       |          |
|       |          |

2.Get all image name from database for particular user and store it in Array
3.Search for image in document directory

    --Update or delete the image from the database and document directory according to user action 

【讨论】:

  • 感谢您提出这个好主意。我以前从未使用过 SQLite 数据库。你能看看我上面的代码并帮我设置吗?我不太确定从哪里开始。
  • 我提出了一个新问题来帮助解决这个问题。请看stackoverflow.com/questions/41042541/…
【解决方案2】:

如果您想将所有图像添加到数组中,您可以这样做:

let imagesArray = [UIImage(named: "img1"), UIImage(named: "img2"), UIImage(named: "img3"), UIImage(named: "img4")]

如果你想附加一张图片,你会这样做:

imagesArray.append(UIImage(named: "imageName"))

【讨论】:

  • 这很容易,但我的图像正在由用户上传。我没有给他们命名。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-28
  • 1970-01-01
  • 1970-01-01
  • 2014-11-14
  • 2013-10-21
相关资源
最近更新 更多