我遇到一个奇怪的现象:导入图像(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,这是意想不到的行为。 –