2015-10-19 163 views
1

我有一个自定义表单元格。在那个表格单元格中,我想显示包含文本的UIImage,这些文本将从服务器获取。我想要我的UI这样的东西。如何在iOS中对图像进行扭曲处理?

enter image description here

我有这个代码

UIBezierPath * imgRect = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 100, 100)]; 
self.textView.textContainer.exclusionPaths = @[imgRect] 

编辑:

上面的代码将在文本视图中创建一个空白区域的图像,但如何适应图像在那个地区?

回答

0

在您的解决方案,你必须创建一个的UIImageView,设置它的起源一样的TextView,然后调整imgRect-S的大小取决于图像的大小,

的SuperView - // ImageView的的上海华和的TextView

  • 的UIImageView {0,0,100,100} //可以说是100×100图像

  • UITextView的{0,0,300,300},排斥的大小 - {0 ,0,100,100} // 300x300是textview的大小,100x100是大小图像,还可以有更大的排斥大小有文本和图像

间漂亮的间距也有第二个解决方案,创造NSAttributedString并在它的前面追加NSTextAttachment,但是当你在做这个,你必须已经有了Image本身,所以如果你异步获取Image,第一个解决方案会更好。

UIImage *img = [UIImage imageNamed:imgName]; 
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init]; 
textAttachment.image = img; 
NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment]; 
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] init]; 
[attrStr appendAttributedString:attrStringWithImage]; 
+0

尺寸图像的将是动态的,以及文字大小 – Techiee

+0

您可以从服务器上获取图像的大小(你应该已经在服务器上知道它了吧?),或者在获取图像后,可以从UIImage获取其大小,然后调整UIImageView的框架和文本视图排除路径,文本大小无关紧要,文字如你所愿。 – ogres

+0

让我试试你的代码只是在那里,如果你的代码不工作,或者它只是卡住 – Techiee

0

我解决它通过添加Imagview到Texview作为子视图

UIBezierPath * imgRect = [UIBezierPath bezierPathWithRect:CGRectMake(0, 10, 100, 100)]; 
cell.tv_post.textContainer.exclusionPaths = @[imgRect]; 
     UIImageView *tv_image =[[UIImageView alloc]initWithFrame:CGRectMake(0, 10, 100, 100)]; 
[tv_image setImageWithURL:[NSURL URLWithString:[IMAGE_BASE_URL stringByAppendingString:user_post.post_video_thumbnail]]placeholderImage:[UIImage imageNamed:@"post_placeholder.png"]]; 
[cell.tv_post addSubview:tv_image];