UIImageView
角半径在cellForRowAtIndexPath
方法内无法正常工作
而我正在使用的是原型单元。
我还尝试 layoutIfNeeded
方法,但遗憾的是,
这是我的代码:
[cell layoutIfNeeded];
client_image=[cell viewWithTag:3];
[client_image setImageWithURL:[NSURL URLWithString:[info objectForKey:PRE_USERIMAGE]] placeholderImage:[UIImage imageNamed:PLACE_HOLDER_IMAGE]];
// client_image=(UIImageView*)[TMGlobalFunction changebgLayer:client_image];
;
// [client_image layoutIfNeeded];
client_image.layer.masksToBounds=YES;
client_image.layer.borderColor=[UIColor whiteColor].CGColor;
client_image.layer.cornerRadius = client_image.frame.size.height/2;
client_image.layer.borderWidth=[TMGlobalFunction IsiPad]?4.0f:2.0f;
我手动设置时效果很好:
client_image.layer.cornerRadius = 50;
但是当我使用以下代码升级 Xcode 7到8 时,它无效:
client_image.layer.cornerRadius = client_image.frame.size.height/2;
知道我为什么会遇到这个有线问题?
更新: 当我打印client_image.frame.size.height
第一次返回1000时,它在Xcode 8中出现问题。滚动后,它会返回正确的值。
答案 0 :(得分:3)
在使用Imageview框架之前调用layoutIfneed和LayoutSubviews。试试以下......为我工作。
client_image=[cell viewWithTag:3];
[cell layoutIfNeeded];
[cell layoutSubviews];
[client_image setImageWithURL:[NSURL URLWithString:[info objectForKey:PRE_USERIMAGE]] placeholderImage:[UIImage imageNamed:PLACE_HOLDER_IMAGE]];
// client_image=(UIImageView*)[TMGlobalFunction changebgLayer:client_image];
;
// [client_image layoutIfNeeded];
client_image.layer.masksToBounds=YES;
client_image.layer.borderColor=[UIColor whiteColor].CGColor;
client_image.layer.cornerRadius = client_image.frame.size.height/2;
client_image.layer.borderWidth=[TMGlobalFunction IsiPad]?4.0f:2.0f;

答案 1 :(得分:1)
在 cellForRow 中,试试这个。
NSURL *url = [NSURL URLWithString:@"http:url..."];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
UIImage *placeholderImage = [UIImage imageNamed:@"your_placeholder"];
__weak UITableViewCell *weakCell = cell;
[cell.imageView setImageWithURLRequest:request
placeholderImage:placeholderImage
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
weakCell.imageView.image = image;
// Write your code here for Rounding corner
[weakCell setNeedsLayout];
} failure:nil];
下面是返回图像的方法,你可以在YourImageView中设置它。
尝试在此方法的sucessblock中为YourImageView提供圆角。
来自AFNetworking文档:
/**
Asynchronously downloads an image from the specified URL request, and sets it once the request is finished. Any previous image request for the receiver will be cancelled.
If the image is cached locally, the image is set immediately, otherwise the specified placeholder image will be set immediately, and then the remote image will be set once the request is finished.
If a success block is specified, it is the responsibility of the block to set the image of the image view before returning. If no success block is specified, the default behavior of setting the image with `self.image = image` is applied.
@param urlRequest The URL request used for the image request.
@param placeholderImage The image to be set initially, until the image request finishes. If `nil`, the image view will not change its image until the image request finishes.
@param success A block to be executed when the image data task finishes successfully. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the image created from the response data of request. If the image was returned from cache, the response parameter will be `nil`.
@param failure A block object to be executed when the image data task finishes unsuccessfully, or that finishes successfully. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the error object describing the network or parsing error that occurred.
*/
- (void)setImageWithURLRequest:(NSURLRequest *)urlRequest
placeholderImage:(nullable UIImage *)placeholderImage
success:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *image))success
failure:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure;
/**
答案 2 :(得分:0)
尝试在willDisplayCell
委托的tableView
中提供转角半径并检查:
-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *) cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
client_image=[cell viewWithTag:3];
[client_image setImageWithURL:[NSURL URLWithString:[info objectForKey:PRE_USERIMAGE]] placeholderImage:[UIImage imageNamed:PLACE_HOLDER_IMAGE]];
//client_image=(UIImageView*)[TMGlobalFunction changebgLayer:client_image];
;
//[client_image layoutIfNeeded];
client_image.clipsToBounds = YES;
client_image.layer.masksToBounds=YES;
client_image.layer.borderColor=[UIColor whiteColor].CGColor;
client_image.layer.cornerRadius = client_image.frame.size.height/2;
client_image.layer.borderWidth=[TMGlobalFunction IsiPad]?4.0f:2.0f;
[cell layoutSubviews];
[cell layoutIfNeeded];
}
答案 3 :(得分:0)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"YOURCELLINDETIFIER";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UIImageView *profile = (UIImageView*)[cell viewWithTag:90]; // your imageview tag
profile.layer.cornerRadius = profile.frame.size.width / 2;
profile.layer.masksToBounds = YES;
profile.layer.borderWidth = 1.0f;
profile.layer.borderColor = [UIColor grayColor].CGColor;
NSURL *url = [NSURL URLWithString:"YOUR URL HERE"; // your URL
[profile sd_setImageWithURL:url completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
if (!error) {
profile.image=image;
}
}];
return cell;
}
答案 4 :(得分:0)
报告client_image
框架的大小不正确,这就是您需要调用layoutIfNeeded
方法的原因。在执行任何cornerRadius之前,只需尝试在[self layoutIfNeeded];
方法内调用viewDidLoad
。
答案 5 :(得分:0)
这应该有用。
#import <QuartzCore/QuartzCore.h>
cell.previewImage.clipsToBounds = YES;
cell.previewImage.layer.cornerRadius = 20;//asper your requirement give the value of radius.