1

UIImageView corner radius not working inside cellForRowAtIndexPath method and and i am using is prototype cell.

I also tried layoutIfNeeded method unfortunately doesn't work.

Here is my code:

[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;

This works fine when I set it manually like:

client_image.layer.cornerRadius = 50; 

But it's not working when i upgrade Xcode 7 to 8 with below code:

client_image.layer.cornerRadius = client_image.frame.size.height/2;

Any idea how why i am getting this wired issue?

Update: it is issue in Xcode 8 when I print client_image.frame.size.height it's return 1000 first time. after scrolling it's return right value.

vaibhav
  • 4,038
  • 1
  • 21
  • 51
balkaran singh
  • 2,754
  • 1
  • 17
  • 32

6 Answers6

3

call layoutIfneed and LayoutSubviews both before using frame of Imageview. Try following..Working for me.

 
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;
PlusInfosys
  • 3,416
  • 1
  • 19
  • 33
1

In your cellForRow, Try this.

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];

Below is the method which will return the image and you can set this in YourImageView.

Try to give round corner to YourImageView in this method's sucessblock.

From AFNetworking Documentation :

/**
 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;

/**
Wolverine
  • 4,264
  • 1
  • 27
  • 49
0

Try to give corner radius in willDisplayCell of tableView delegate and check:

-(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];
}
Ronak Chaniyara
  • 5,335
  • 3
  • 24
  • 51
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;
}
Himanshu Moradiya
  • 4,769
  • 4
  • 25
  • 49
  • i get the image right. but issue with it's cornerRadius . – balkaran singh Oct 21 '16 at 12:08
  • update your question with your - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { method that return your tableviewcell hight } and also update your Runtime UI – Himanshu Moradiya Oct 21 '16 at 12:12
0

Size of the client_image frame is reported incorrect thats why you need to call layoutIfNeeded method. Just try calling [self layoutIfNeeded]; inside viewDidLoad method before doing any cornerRadius stuff.

vaibhav
  • 4,038
  • 1
  • 21
  • 51
  • it's not working it's work if i use image in uiviewcontroller not in cell. – balkaran singh Oct 21 '16 at 12:17
  • i am not using any custom class for tableview cell. – balkaran singh Oct 21 '16 at 12:22
  • but surely its a layout issue, try getting the value before passing to `cornerRadius` directly then i think 1000 is big figure, you are almost done help yourself. – vaibhav Oct 21 '16 at 12:25
  • @balkaransingh your issue is around here http://stackoverflow.com/questions/39503661/cornerradius-stopped-working-in-swift-2-3-ios-10-xcode-8 see all the ans. – vaibhav Oct 21 '16 at 12:41
0

This should work.

#import <QuartzCore/QuartzCore.h>

cell.previewImage.clipsToBounds = YES;
cell.previewImage.layer.cornerRadius = 20;//asper your requirement give the value of radius.