美文网首页
dataWithContentsOfURL:失效??

dataWithContentsOfURL:失效??

作者: wustzhy | 来源:发表于2023-01-02 14:50 被阅读0次

    /private/var/mobile/Containers/Data/Application/路径下的图片,无法通过[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_imageUrl]]]得到图片,然而[UIImage imageWithContentsOfFile:_imageUrl]就可以

    (lldb) po _imageUrl
    /private/var/mobile/Containers/Data/Application/E62F5EC0-5752-44A6-807D-85730189832E/tmp/ReactNative/ED8D8130-2C91-4A26-9CFC-57BFF0E2BF62.jpg
    
    (lldb) po [NSData dataWithContentsOfURL:[NSURL URLWithString:_imageUrl]]
    2023-01-03 12:36:38.518437+0800 MyApp[4267:3025225] NSURLConnection finished with error - code -1002
     nil
     
    (lldb) po [NSData dataWithContentsOfFile:_imageUrl]
      po [NSData dataWithContentsOfFile:_imageUrl]
    <ffd8ffe0 00104a46 49460001 01000090 00900000 ffe10080 45786966 00004d4d 002a0000 00080005 01120003 00000001 
    ...
    
    

    搜到 iOS:关于沙盒路径,缓存/视频,存储规则和使用

    原来 沙盒路径尽量用 fileURLWithPath:, 别用URLWithString:

    po [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:_imageUrl]]
    <ffd8ffe0 00104a46 49460001 01000090 00900000 ffe10080 45786966 00004d4d 002a0000 00080005 01120003 00000001 00010000 011a0005 00000001 0000004a 011b0005 00000001 00000052 01280003 00000001 00020000 87690004 00000001 0000005a 00000000 00000090 00000001 00000090 00000001 0002a002 00040000 00010000 0280a003 00040000 00010000 0eb30000 0
    

    对比 fileURLWithPath:URLWithString:

    (lldb) po _imageUrl
    /private/var/mobile/Containers/Data/Application/E62F5EC0-5752-44A6-807D-85730189832E/tmp/ReactNative/ED8D8130-2C91-4A26-9CFC-57BFF0E2BF62.jpg
    
    (lldb) po [NSURL fileURLWithPath:_imageUrl]
    file:///private/var/mobile/Containers/Data/Application/E62F5EC0-5752-44A6-807D-85730189832E/tmp/ReactNative/ED8D8130-2C91-4A26-9CFC-57BFF0E2BF62.jpg
    
    
    (lldb) po [NSURL URLWithString:_imageUrl]
    /private/var/mobile/Containers/Data/Application/E62F5EC0-5752-44A6-807D-85730189832E/tmp/ReactNative/ED8D8130-2C91-4A26- ... BF62.jpg
    
    (lldb) po [NSURL URLWithString:_imageUrl].absoluteString
    /private/var/mobile/Containers/Data/Application/E62F5EC0-5752-44A6-807D-85730189832E/tmp/ReactNative/ED8D8130-2C91-4A26-9CFC-57BFF0E2BF62.jpg
    
    • 沙盒路径 转url,使用fileURLWithPath 会生成协议头file://,最终正常得到image。而URLWithString则最终获取image位null

    • 网络链接 转url,则反之
      使用fileURLWithPath 则不合理了,也无法正常得到image,使用URLWithString才OK

    (lldb) po [NSURL fileURLWithPath:@"https://profile.csdnimg.cn/C/4/8/1_wustzhy2016"]
    https:/profile.csdnimg.cn/C/4/8/1_wustzhy2016 -- file:///
    
    (lldb) po [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL fileURLWithPath:@"https://profile.csdnimg.cn/C/4/8/1_wustzhy2016"]]]
     nil
    
    (lldb) po [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://profile.csdnimg.cn/C/4/8/1_wustzhy2016"]]]
    <UIImage:0x2805b07e0 anonymous {150, 150} renderingMode=automatic(original)>
    

    相关文章

      网友评论

          本文标题:dataWithContentsOfURL:失效??

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