美文网首页
Swift 实现对象类的==重载以及iOS相册和相机图片选择问

Swift 实现对象类的==重载以及iOS相册和相机图片选择问

作者: lotawei | 来源:发表于2017-07-05 11:54 被阅读22次
        var type: ModelType?
        var data: PHAsset?
       init(type: ModelType?,data:PHAsset?){
            self.type = type
            self.data = data
        }
          static func ==(lhs: customModel, rhs: customModel) -> Bool {
            return lhs.type == rhs.type && lhs.data == rhs.data
       }}```
    实现 Equatable的协议
    在检测重复图片时,for循环遍历时 不要将插入操作放入遍历中,不然超级卡顿,访问相册时卡的原因 我遇到的就是这个原因
      ```  for  model in  selectModel{
                if  model == newmodel{
                    return
               }
           selectModel.insert(newmodel, at: 0)
    }```
    存入系统相册
    ``` func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
           let   img  = info[ UIImagePickerControllerOriginalImage] as!  UIImage       
           PHPhotoLibrary.shared().performChanges({
            let   req = PHAssetChangeRequest.creationRequestForAsset(from: img)  self.cameraidentitifer.append((req.placeholderForCreatedAsset?.localIdentifier)
           }) { (success, err) in
            if success {
                //  加上这句 可以秒刷新视图
                DispatchQueue.main.async {
                    self.updateCameraimgs()
                }
            }
           }
            //   将资源放入 当前的cell中
            dismiss(animated: true, completion: nil)
        }```
    
    //根据localindentity更新内容
    ```  if  self.cameraidentitifer.count == 0 {
                return
            }
            let   fetchresult = PHAsset.fetchAssets(withLocalIdentifiers: self.cameraidentitifer, options: nil)
      fetchresult.enumerateObjects({ (assert,pos, stop) in
                var  phasets = [PHAsset]()
                phasets.append(assert)
      //  加上这句 可以秒刷新视图
                DispatchQueue.main.async {
                    self.onImageSelectFinished(images: phasets)
                    self.cameraidentitifer.removeAll()
                }
            })```

    相关文章

      网友评论

          本文标题:Swift 实现对象类的==重载以及iOS相册和相机图片选择问

          本文链接:https://www.haomeiwen.com/subject/aztqhxtx.html