我遇到一個奇怪的現象:導入圖像(SWIFT)
的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)
}
的數據是零!我做錯了什麼,URL的字符串在兩種情況下都是相同的!
您是否嘗試過使用斷點,看一步一步,它產生了意想不到的結果? – d00dle
A類:url_a - > string_a - > data - >圖像,但B類:string_a - > data - > nil,這是意想不到的行爲。 –