私は奇妙な行動が発生しました:画像読み込み中(スウィフト)
アンimagePickerはPHAssetを返し、私は次の操作を行うと、データから画像をImageViewのを提示するために管理します。
asset.requestContentEditingInput(with: PHContentEditingInputRequestOptions()) { (input, _) in
let url = input?.fullSizeImageURL
let imgV = UIImageView()
let test_url = URL(string: (url?.absoluteString)!)
print("><><><^^^><><>\(test_url)")
//prints: ><><><^^^><><>Optional(file:///var/mobile/Media/DCIM/107APPLE/IMG_7242.JPG)
let data = NSData(contentsOf: test_url! as URL)
imgV.image = UIImage(data: data! as Data)
imgV.backgroundColor = UIColor.cyan
att.imageLocalURL = url?.absoluteString//// saving the string to use in the other class
imgV.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
self.view.addSubview(imgV) /// just to test that the file exists and can produce an image
は、しかし、私は別のクラスでは、次の手順を実行したとき:
if((NSURL(string: self.attachment.imageLocalURL!) as URL!).isFileURL)// checking if is Local URL
{
let test_url = URL(string: self.attachment.imageLocalURL!) // reading the value stored from before
print("><><><^^^><><>\(test_url)")
//prints :><><><^^^><><>Optional(file:///var/mobile/Media/DCIM/107APPLE/IMG_7242.JPG)
let data = NSData(contentsOf: test_url! as URL)
imageView.image = UIImage(data: data! as Data)
}
データがnilであります!私は間違って何をしていますか?URLの文字列はどちらの場合も同じです!
を保持しますが、ブレークポイントをしようとすると、ステップ・バイ・ステップ、それは予期しない結果を生成する見ましたか? – d00dle
クラスA:url_a - > string_a - > data - > imageですが、クラスB:string_a - > data - > nil、つまり予期しない動作です。 –