2016-11-15 2 views
1

私は、ユーザから収集した画像の配列を作成しています。この配列は、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) 
} 
+1

コードをコピーして元に戻し、画像を削除してください。 – shallowThought

+0

コードをコピーして貼り付けました。 – Allie

+0

^@shallowThought – Allie

答えて

0

あなたは、単にこれを行うことができ、アレイにすべての画像を追加したい場合は:

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

あなたが画像を追加したい場合、これはあなたがそれを行うだろうかです:

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

これは簡単ですが、私の画像はユーザーによってアップロードされています。私はそれらの名前を持っていない。 – Allie

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 
+0

この素晴らしいアイデアをありがとう。以前はSQLiteデータベースで作業していませんでした。上のコードを見て、私がそれを設定するのを手伝ってもらえますか?どこから始めたらいいか分からない。 – Allie

+0

私はこれを助ける新しい質問をしました。 http://stackoverflow.com/questions/41042541/building-sqlite-database-with-photosをご覧ください。 – Allie

関連する問題