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

2015-09-17 16:33:50 +08:00
 lufir

代码如下:
```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 就无法显示了

---求助各路大神
2077 次点击
所在节点    iOS
1 条回复
wujichao
2015-09-17 17:10:36 +08:00
哥们你是新手吧 下载是异步的, 这时候 image 还没下好呢

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/221489

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX