Ios 创建收集的图像数组
我正在创建从用户收集的图像数组。此阵列用于在UICollectionView中显示图像。我正在将图像收集/存储在一个名为ImageStore.swift的文件中,请参见下文。另外,请参见“我的UICollection”视图中的数组。我应该在数组中放入什么来传递图像 具有UICollectionView的视图控制器:Ios 创建收集的图像数组,ios,arrays,swift,uicollectionview,photo,Ios,Arrays,Swift,Uicollectionview,Photo,我正在创建从用户收集的图像数组。此阵列用于在UICollectionView中显示图像。我正在将图像收集/存储在一个名为ImageStore.swift的文件中,请参见下文。另外,请参见“我的UICollection”视图中的数组。我应该在数组中放入什么来传递图像 具有UICollectionView的视图控制器: class PhotosViewController: UIViewController, UICollectionViewDelegate, UICollectionVie
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)
}
如果要将所有图像添加到阵列中,只需执行以下操作:
let imagesArray = [UIImage(named: "img1"), UIImage(named: "img2"), UIImage(named: "img3"), UIImage(named: "img4")]
如果要附加图像,请执行以下操作:
imagesArray.append(UIImage(named: "imageName"))
对于大型数据集,最好的方法是使用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
请复制代码并删除图像。我复制并粘贴了代码。^@shallow尽管我有点困惑。是否要知道在集合视图文件中放置阵列的位置?或者您想知道如何将图像放入阵列?我想知道如何将图像放入阵列@Calebkleveter这将很容易,但我的图像是由用户上传的。我没有名字。谢谢你这个好主意。我以前从未使用过SQLite数据库。你能看一下我上面的代码并帮我设置一下吗?我不太确定从哪里开始。我提出了一个新问题来帮助解决这个问题。请看