我正在开发项目,如Instagram上传图像并显示主页中的所有Feed。但图像不适合图像视图。在Instagram表视图单元格中根据图像大小自动调整,请提供建议。
答案 0 :(得分:1)
[_imageView setImage:[ViewController imageWithContentMode:[UIImage imageNamed:@"howwehelp_bg.png"] withScaledSize:_imageView.bounds.size]];
_imageView.contentMode = UIViewContentModeScaleToFill;
+(UIImage *)imageWithContentMode:(UIImage *)image withScaledSize:(CGSize)newSize{
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *imageNew = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return imageNew;
}
答案 1 :(得分:0)
您可以将此代码应用于合适的图像。
+(UIImage*)imageWithImage: (UIImage*) sourceImage scaledToWidth: (float) i_width
{
float oldWidth = sourceImage.size.width;
float scaleFactor = i_width / oldWidth;
float newHeight = sourceImage.size.height * scaleFactor;
float newWidth = oldWidth * scaleFactor;
UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight));
[sourceImage drawInRect:CGRectMake(0, 0, newWidth, newHeight)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}