2015-10-19 51 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];