V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
lufir
V2EX  ›  iOS

求助: ios 如何用 NSTextattAchment 加载 SDWebImage 下载下来的图片

  •  
  •   lufir · 2015-09-17 16:33:50 +08:00 · 2074 次点击
    这是一个创建于 3163 天前的主题,其中的信息可能已经有所发展或是发生改变。

    代码如下:
    ```objc

    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake (0, 20, self.view.frame.size.width, self.view.frame.size.height-20 )];
    [self.view addSubview:textView];
    
    UIImageView *imageView = [[UIImageView alloc] init];
    [imageView sd_setImageWithURL:[NSURL URLWithString:@"http://7bv920.com1.z0.glb.clouddn.com/20150901.jpg"]];
    
    NSTextAttachment *attachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
    attachment.image = imageView.image;
    attachment.bounds = CGRectMake (0, 20, self.view.frame.size.width, 300 );
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithAttributedString:textView.attributedText];
    NSAttributedString *imageStr = [NSAttributedString attributedStringWithAttachment:attachment];
    [attStr appendAttributedString:imageStr];
    textView.attributedText = attStr;
    
    1.图片加载是正确的,如果直接将 imageview 显示在 self.view 上可以显示。
    2.用 NSTextAttachment 直接加载一张本地图片也可以正常显示
    3.用 NSTextattAchment 加载 imageview.image 就无法显示了
    
    ---求助各路大神
    
    wujichao
        1
    wujichao  
       2015-09-17 17:10:36 +08:00   ❤️ 1
    哥们你是新手吧 下载是异步的, 这时候 image 还没下好呢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1161 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 18:36 · PVG 02:36 · LAX 11:36 · JFK 14:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.