我的图像高度为600,宽度也是600现在我想以100x100的形式在UIImageView中以编程方式显示该图像。我尝试但是图像不像原始图像我添加图像的屏幕截图首先是100x100,第二个是200x200。第二个很好。但是第一个不像原始图像。知道我做什么请
in.h中
@interface ViewController : UIViewController
{
IBOutlet UIImageView *imageView1;
IBOutlet UIImageView *imageView2;
}
@end
In.m
- (void)viewDidLoad
{
[super viewDidLoad];
imageView1.frame=CGRectMake(100, 50, 100, 100);
imageView2.frame=CGRectMake(50, 200, 200, 200);
imageView1.image = [UIImage imageNamed:@"Untitled-7.png"];
imageView2.image = [UIImage imageNamed:@"Untitled-7.png"];
}
答案 0 :(得分:18)
请尝试此
UIImage *originalImage = [UIImage imageNamed:@"5.png"];
CGSize destinationSize = CGSizeMake(100, 100);
UIGraphicsBeginImageContext(destinationSize);
[originalImage drawInRect:CGRectMake(0,0,destinationSize.width,destinationSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *newImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];
newImageView.image = newImage;
[self.view addSubview:newImageView];
答案 1 :(得分:5)
尝试imageView1.contentMode = UIViewContentModeScaleAspectFit;
答案 2 :(得分:4)
使用此方法:
- (UIImage*)imageWithImage:(UIImage*)image
scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
调用方法:
CGSize newSize = CGSizeMake(100.0,100.0);
[self imageWithImage:image scaledToSize:newSize];
答案 3 :(得分:3)
设置内容模式
imageView2.contentMode = UIViewContentModeScaleAspectFit;
答案 4 :(得分:3)
在swift 3中
func createImage(withImage image: UIImage, forSize size: CGSize) -> UIImage? {
UIGraphicsBeginImageContext(size)
image.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
if let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext() {
UIGraphicsEndImageContext()
return newImage;
}
return nil
}
答案 5 :(得分:1)
点击此链接以编程方式调整图片大小
http://developer.appcelerator.com/question/121517/how-to-scale-image-proportionally http://iphonedevsdk.com/forum/iphone-sdk-development/95666-resize-uiimageview-proportionally.html
答案 6 :(得分:1)
从Swift 4.2开始,
使用此选项,您将获得正确宽高比的非模糊调整大小图像-
class func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage {
let size = image.size
let widthRatio = targetSize.width / size.width
let heightRatio = targetSize.height / size.height
// Figure out what our orientation is, and use that to form the rectangle
var newSize: CGSize
if(widthRatio > heightRatio) {
newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
} else {
newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
}
// This is the rect that we've calculated out and this is what is actually used below
let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)
//resizing the rect using the ImageContext
UIGraphicsBeginImageContextWithOptions(newSize, false, 0)
image.draw(in: rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
根据开发人员文档,以下方法使用指定的选项创建基于位图的图形上下文-
func UIGraphicsBeginImageContextWithOptions(_ size:CGSize,_ opaque:Bool,_ scale:CGFloat)
参数-
大小: 新位图上下文的大小(以磅为单位)。这表示由UIGraphicsGetImageFromCurrentImageContext()函数返回的图像的大小。
不透明: 一个布尔型标志,指示位图是否不透明。
规模: 应用于位图的比例因子。如果您将值指定为0.0,则比例因子将设置为设备主屏幕的比例因子。
参考资料-https://developer.apple.com/documentation/uikit/1623912-uigraphicsbeginimagecontextwitho
答案 7 :(得分:0)
使用图像初始化imageView,然后调整其大小
imageView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Untitled-7.png"]];
imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Untitled-7.png"]];
[imageView1 setFrame:CGRectMake(100, 50, 100, 100)];
[imageView2 setFrame:CGRectMake(50, 200, 200, 200)];
尝试一下,让我知道它是否有效!
答案 8 :(得分:0)
试试这个: -
- (void)viewDidLoad
{
[super viewDidLoad];
imageView1.frame=CGRectMake(100, 50, 100, 100);
imageView2.frame=CGRectMake(50, 200, 200, 200);
imageView1.contentMode = UIViewContentModeScaleAspectFit;
imageView2.contentMode = UIViewContentModeScaleAspectFit;
imageView1.image = [UIImage imageNamed:@"Untitled-7.png"];
imageView2.image = [UIImage imageNamed:@"Untitled-7.png"];
}
享受。
答案 9 :(得分:0)
尝试更改此内容:
imageView1.image = [UIImage imageNamed:@"Untitled-7.png"];
对此:
imageView1.image = [[UIImage imageNamed:@"Untitled-7.png"] resizableImageWithCapInsets:UIEdgeInsetsZero
resizingMode:UIImageResizingModeStretch];
答案 10 :(得分:0)
我喜欢在UIImage上使用类别,方法如下:
// Returns a rescaled copy of the image, taking into account its orientation
// The image will be scaled disproportionately if necessary to fit the bounds specified by the parameter
- (UIImage *)resizedImage:(CGSize)newSize interpolationQuality:(CGInterpolationQuality)quality {
CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width * self.scale, newSize.height * self.scale));
UIImage *newImage;
UIGraphicsBeginImageContextWithOptions(newRect.size, NO, 0.0);
newImage = [UIImage imageWithCGImage:[self CGImage] scale:0.0 orientation:self.imageOrientation];
[newImage drawInRect:newRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
请参阅此updated repo和此blog post。
这适用于调整PDF图像的大小,特别是如果你需要在UIBarButtonItem中添加一个。
答案 11 :(得分:0)
嘿,如果您已经在使用Alamofire图像库,那么您可以使用以下代码:
UIImage(named:"image").af_imageScaled(to: CGSize(width: 100, height: 100))