将图像从手机图库显示到collectionViews中的单元格

时间:2016-09-08 10:30:58

标签: ios swift uiimagepickercontroller collectionview

 func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    imagePicker.allowsEditing =  false
    imagePicker.sourceType = .PhotoLibrary
    self.presentViewController(imagePicker, animated: true, completion: nil)



}
 //  func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
   // picker .dismissViewControllerAnimated(true, completion: nil)
 // }
       func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {


    }

单击单元格,我能够打开图库,我需要保存图像并将其显示在单元格中,有什么帮助吗?

2 个答案:

答案 0 :(得分:1)

您需要使用“AssetsLibrary”框架从设备获取图像。

您可以在以下链接中找到解决方案 - Solution link 在iOS 9.0中不推荐使用AssetsLibrary

使用了新的“Photo”框架。

这是使用新框架的快速代码。

func getAllImages() {

    let imgManager     : PHImageManager!        = PHImageManager.defaultManager()
    let photosArray    : NSMutableArray!        = NSMutableArray()
    let requestOptions : PHImageRequestOptions! = PHImageRequestOptions()
    let fetchOptions   : PHFetchOptions!        = PHFetchOptions()

    requestOptions.synchronous = true

    if let fetchResult: PHFetchResult = PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Image, options: fetchOptions) {

        if fetchResult.count > 0 {

            for i in 0...fetchResult.count {
                imgManager.requestImageForAsset(fetchResult.objectAtIndex(i) as! PHAsset, targetSize: view.frame.size, contentMode: PHImageContentMode.AspectFill, options: requestOptions, resultHandler: { (image, _) in
                    photosArray.addObject(image!)
                });
            }
        }
    }
}

您需要使用此代码包含标题 - “导入照片”

答案 1 :(得分:0)

var pickedImage = UIImageView()

pickedImage = info [UIImagePickerControllerOriginalImage]为?的UIImage

在CollectionView的cellForRowAtIndexPath方法

cell.imageview.image = pickedImage