2015-04-21 141 views
0

我已經完成了此操作,但由於某種原因它不起作用。我試圖在我的桌面視圖中有圓形圖片,除了一件事以外,一切正常:將所有圖像的大小調整爲相等大小

圖片不是全部大小相同,因此舍入不能正常工作,有些照片只是荒謬的。我正在使用下面的代碼。

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:identifier]; 

if (cell == nil) { 
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; 
} 

cell.imageView.layer.masksToBounds = YES; 
cell.imageView.clipsToBounds = YES;; 
cell.imageView.layer.cornerRadius = cell.imageView.layer.frame.size.width/2; 

//Getting the picture data of the current contact 
NSData *pictureData = [[[[dataArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]picture]data]; 
UIImage *picture; 

//Setting the picture or a placeholder if none was found 
if (pictureData == nil || pictureData.length < 15){ 
    picture = [UIImage imageNamed:@"placeholder.png"]; 
}else{ 
    picture = [UIImage imageWithData:pictureData]; 
} 

cell.textLabel.text = [[[dataArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]compositeName]; 
cell.imageView.image = picture; 

return cell; 

我該怎麼做才能在任何時候都有圓形圖像考慮一些imageviews(顯然是...)是不同的大小。

注意,實現代碼如下細胞和圖像都是平等的高度/寬度的,他們只是得到一個cornerradius值不是他們的高度的一半明顯,但他們仍然不寬或更高。

回答

0

之前添加的UIImage到了UIImageView的,我會建議你將圖像尺寸調整到正確的尺寸(的UIImageView的大小)。

這樣的圖像將被舍和滾動會更好時的性能。

爲了調整你可以使用下面的方法圖像:

- (UIImage*) scaleImage:(UIImage*)image toSize:(CGSize)newSize { 
    CGSize scaledSize = newSize; 
    float scaleFactor = 1.0; 
    if(image.size.width > image.size.height) { 
     scaleFactor = image.size.width/image.size.height; 
     scaledSize.width = newSize.width; 
     scaledSize.height = newSize.height/scaleFactor; 
    } 
    else { 
     scaleFactor = image.size.height/image.size.width; 
     scaledSize.height = newSize.height; 
     scaledSize.width = newSize.width/scaleFactor; 
    } 

    UIGraphicsBeginImageContextWithOptions(scaledSize, NO, 0.0); 
    CGRect scaledImageRect = CGRectMake(0.0, 0.0, scaledSize.width, scaledSize.height); 
    [image drawInRect:scaledImageRect]; 
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext();  

    return scaledImage; 
} 

而且使用這樣的:

cell.imageView.image = [self scaleImage:picture toSize:CGSizeMake(width, height)];