缩放单元格的图像

时间:2014-03-19 11:04:45

标签: ios objective-c

我正在尝试将图像添加到单元格,它可以工作,但缩放不是真的有效,图像缩放到某个大小,并保持在那个大小,我输入到缩放功能的数字不会改变一件事。(图像是自动缩放到某个恒定大小)

 NSURL *url = [NSURL URLWithString:icon];
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *logo=[UIImage imageWithData:data scale:4];
        cell.imageView.layer.masksToBounds = YES;
        cell.imageView.layer.cornerRadius = 10.0;
        cell.imageView.image=logo;

3 个答案:

答案 0 :(得分:0)

实施此方法并使用它:

/*! Returns a UIImage which is resized image of the image provided.
    @param image
        The image to be resized.
    @param size
        The size of the image to be resized.
*/
+ (UIImage*)resizeImage:(UIImage *)image imageSize:(CGSize)size {
    CGFloat imageWidth = image.size.width;
    CGFloat imageHeight = image.size.height;
    CGFloat requiredWidth = (imageWidth * size.height) / imageHeight;

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(requiredWidth, size.height), NO, [UIScreen mainScreen].scale);
    [image drawInRect:CGRectMake(0, 0, requiredWidth, size.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    //here is the scaled image which has been changed to the size specified
    UIGraphicsEndImageContext();
    return newImage;
}

答案 1 :(得分:0)

只需在声明如下的自定义单元格类方法中编写掩码函数。

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state

    [self.imageView.layer setCornerRadius:10.0];
    self.imageView.layer.masksToBounds = YES;
}

答案 2 :(得分:0)

使用此功能可根据您的要求缩放图像。

- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    //UIGraphicsBeginImageContext(newSize);
    float theScaleFactor = 0.0f;
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
        if ([[UIScreen mainScreen] scale] == 2.0) {
            theScaleFactor = 2.0f;
        } else {
            theScaleFactor = 0.0f;
        }
    } else {
        theScaleFactor = 0.0f;
    }

    UIGraphicsBeginImageContextWithOptions(newSize, NO, theScaleFactor);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

希望这会对您有所帮助。