[英]Rounded Rect's in custom cell, UITableViewController while maintaining aspect ratio
當引用帶有圓角的UIImage時,我能夠創建具有圓角矩形的圖像。 這無疑幫助了我的滾動刷新率。
這個問題我現在是,我一般用UIViewContentModeScaleAspectFill
同時提請我UIImages
進入視野的。
使用UIBezierPath
繪制,然后將圖像置於其邊界內UIBezierPath
圖像失去其寬高比。
[UIBezierPath bezierPathWithRoundedRect:imageView.bounds
cornerRadius:10.0] addClip];
[image drawInRect:imageView.bounds];
編寫自定義比例尺解決方案是唯一的出路嗎?
謝謝
GSize size1 = image.size;
CGSize size2 = imageView.bounds.size;
CGRect newRect;
if (size1.width / size1.height > size2.width / size2.height) {
CGFloat newWidth = size2.height * size1.width / size1.height;
newRect = CGRectMake((size2.width - newWidth)/2, 0, newWidth, size2.height);
} else {
CGFloat newHeight = size1.height / size1.width * size2.width;
newRect = CGRectMake(0, (size2.height - newHeight)/2, size2.width, newHeight);
}
[image drawInRect:newRect];
imageView.image = UIGraphicsGetImageFromCurrentImageContext();
問題在於,您引用的方法使用圖像視圖的邊界作為圖像上下文的大小,並且它可能沒有正確的寬高比,因此您需要先設置圖像視圖的大小,然后再傳遞它。 ,如果您希望所有圖像視圖都具有相同的寬度,請傳入該寬度以及一個由該寬度和圖像的縱橫比確定的高度。
CGFloat高度= imageView.bounds.size.width *(image.size.width / image.size.height);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.