Objective c 如何按比例缩放UIImageView?
我有一个UIImageView,目标是通过给它一个高度或宽度来按比例缩小它Objective c 如何按比例缩放UIImageView?,objective-c,cocoa-touch,Objective C,Cocoa Touch,我有一个UIImageView,目标是通过给它一个高度或宽度来按比例缩小它 UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://farm4.static.flickr.com/3092/2915896504_a88b69c9de.jpg"]]]; UIImageView *imageView = [[UIImageView alloc]
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://farm4.static.flickr.com/3092/2915896504_a88b69c9de.jpg"]]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
//Add image view
[self.view addSubview:imageView];
//set contentMode to scale aspect to fit
imageView.contentMode = UIViewContentModeScaleAspectFit;
//change width of frame
CGRect frame = imageView.frame;
frame.size.width = 100;
imageView.frame = frame;
图像确实调整了大小,但位置不在左上角。缩放图像/图像视图的最佳方法是什么?如何校正位置?我认为您可以执行以下操作
image.center = [[imageView window] center];
您可以尝试使
图像视图
大小与图像
匹配。以下代码未经测试
CGSize kMaxImageViewSize = {.width = 100, .height = 100};
CGSize imageSize = image.size;
CGFloat aspectRatio = imageSize.width / imageSize.height;
CGRect frame = imageView.frame;
if (kMaxImageViewSize.width / aspectRatio <= kMaxImageViewSize.height)
{
frame.size.width = kMaxImageViewSize.width;
frame.size.height = frame.size.width / aspectRatio;
}
else
{
frame.size.height = kMaxImageViewSize.height;
frame.size.width = frame.size.height * aspectRatio;
}
imageView.frame = frame;
CGSize-kMaxImageViewSize={.width=100.height=100};
CGSize imageSize=image.size;
CGFloat aspectRatio=imageSize.width/imageSize.height;
CGRect frame=imageView.frame;
如果(kMaxImageViewSize.width/aspectRatio,以下是可以轻松缩放的方法
这在2.x中与模拟器和iPhone一起工作
UIImage *thumbnail = [originalImage _imageScaledToSize:CGSizeMake(40.0, 40.0) interpolationQuality:1];
我刚刚尝试了这个,UIImage不支持_imageScaledToSize
最后,我使用一个类别向UIImage添加了一个方法——这是我在Apple开发论坛上找到的建议
在项目范围内-
@interface UIImage (Extras)
- (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize;
@end;
实施:
@implementation UIImage (Extras)
- (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize {
UIImage *sourceImage = self;
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
if (CGSizeEqualToSize(imageSize, targetSize) == NO) {
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if (widthFactor < heightFactor)
scaleFactor = widthFactor;
else
scaleFactor = heightFactor;
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
// center the image
if (widthFactor < heightFactor) {
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
} else if (widthFactor > heightFactor) {
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
// this is actually the interesting part:
UIGraphicsBeginImageContext(targetSize);
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if(newImage == nil) NSLog(@"could not scale image");
return newImage ;
}
@end;
@实现UIImage(附加)
-(UIImage*)按比例缩放图像大小:(CGSize)目标大小{
UIImage*sourceImage=self;
UIImage*newImage=nil;
CGSize imageSize=sourceImage.size;
CGFloat width=imageSize.width;
CGFloat height=图像大小.height;
CGFloat targetWidth=targetSize.width;
CGFloat targetHeight=targetSize.height;
CGFloat scaleFactor=0.0;
CGFloat scaledWidth=targetWidth;
CGFloat缩放高度=目标高度;
CGPoint thumbnailPoint=CGPointMake(0.0,0.0);
if(CGSizeEqualToSize(imageSize,targetSize)==否){
CGFloat宽度系数=目标宽度/宽度;
CGFloat高度系数=目标高度/高度;
if(宽度系数<高度系数)
scaleFactor=宽度因子;
其他的
比例因子=高度因子;
scaledWidth=宽度*缩放因子;
缩放高度=高度*缩放因子;
//将图像居中
if(宽度系数<高度系数){
thumbnailPoint.y=(targetLight-缩放高度)*0.5;
}否则如果(宽度因子>高度因子){
thumbnailPoint.x=(targetWidth-缩放宽度)*0.5;
}
}
//这实际上是有趣的部分:
UIGraphicsBeginImageContext(targetSize);
CGRect thumbnailRect=CGRectZero;
thumbnailRect.origin=thumbnailPoint;
thumbnailRect.size.width=缩放宽度;
thumbnailRect.size.height=缩放高度;
[sourceImage drawInRect:thumbnailRect];
newImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
如果(newImage==nil)NSLog(@“无法缩放图像”);
返回新图像;
}
@结束;
一旦找到文档,就可以轻松修复
imageView.contentMode = .scaleAspectFit
可以用这种方法调整UIImage的大小
image = [UIImage imageWithCGImage:[image CGImage] scale:2.0 orientation:UIImageOrientationUp];
在iOS 4.2中,贴在顶部的原始代码对我来说运行良好
我发现在我的例子中,创建一个CGRect并指定所有的顶部、左侧、宽度和高度值是调整位置的最简单的方法,这是在表格单元格中使用UIImageView。(仍然需要添加代码来释放对象)我看到了一些关于比例类型的对话,所以我决定把一个
相关图像如下所示:
UIImageView+Scale.h:
#import <Foundation/Foundation.h>
@interface UIImageView (Scale)
-(void) scaleAspectFit:(CGFloat) scaleFactor;
@end
通过选择模式将ImageView设置为Aspect Fill
,并选中Clip子视图
框
对于Swift:
self.imageViews.contentMode = UIViewContentMode.ScaleToFill
我使用了以下代码
if (image.size.height<self.imageCoverView.bounds.size.height && image.size.width<self.imageCoverView.bounds.size.width)
{
[self.profileImageView sizeToFit];
self.profileImageView.contentMode =UIViewContentModeCenter
}
else
{
self.profileImageView.contentMode =UIViewContentModeScaleAspectFit;
}
if(image.size.height通常我在应用程序中使用这种方法(Swift 2.x兼容):
这对我来说很好Swift 2.x:
imageView.contentMode = .ScaleAspectFill
imageView.clipsToBounds = true;
按比例设置您的UIimageview
如果这里提出的解决方案对您不起作用,而您的图像资产实际上是PDF,请注意,XCode实际上对待PDF的方式与对待图像文件的方式不同。特别是,它似乎无法通过缩放来正确填充PDF:它最终被平铺。这让我发疯,直到我发现问题是PDF格式。Co返回到JPG,您应该准备好了。您好,您知道interpolationQuality:参数的作用吗?感谢这是一个未记录的方法(下划线是一个完全免费的方法)这可能会导致应用程序被拒绝。interpolationQuality参数会做什么?它不会缩放图像,只会将其居中。我有一些类似于您的代码,但对我来说不适用“UIImage*image=[UIImage ImageName:imageString];UIImageView*imageView=[[UIImage alloc]initWithImage:image];”t通过此“由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:'-[UIImage initWithImage::]:未识别的选择器发送到实例0xd815930'终止我的应用程序的验证”@Spire这是UIImageView而不是UIImage^^^这对我有用。记住将.contentMode设置为你的UIImageView--而不是你的UIImage。--这是我的:UIImageView*imageView=[[UIImageView alloc]initWithFrame:cDirectMake(0,0,30,30)]你实际上回答了什么?在罗尼的问题中,他提到他使用了它。你的imageView可能没有被剪裁。试试imageView.layer.masksToBounds=是的;我第一次尝试IB以确保它能按需要工作,但我不记得要设置的变量名。所以我搜索了它并找到了这个答案。对于在Swift中有相同问题的人: ScaleAspectFit
现在是一个枚举UIViewContentMode
,因此您可以设置imageView.contentMode=UIViewContentMode.ScaleAspectFit
。请注意这段时间。TranslatesAutoResizengMaskinToConstraints
不应设置为false
。我花了很长时间才意识到此属性正在阻止恢复izeh设置AspectFit时如何垂直对齐?@esbenr这应该会自动发生。您可能可以根据需要找到覆盖,但我目前不知道有覆盖:|我想使用Aspect Fill,但我也想显示完整图像。然后我需要
self.imageViews.contentMode = UIViewContentMode.ScaleToFill
if (image.size.height<self.imageCoverView.bounds.size.height && image.size.width<self.imageCoverView.bounds.size.width)
{
[self.profileImageView sizeToFit];
self.profileImageView.contentMode =UIViewContentModeCenter
}
else
{
self.profileImageView.contentMode =UIViewContentModeScaleAspectFit;
}
// Resize UIImage
func resizeImage(image:UIImage, scaleX:CGFloat,scaleY:CGFloat) ->UIImage {
let size = CGSizeApplyAffineTransform(image.size, CGAffineTransformMakeScale(scaleX, scaleY))
let hasAlpha = true
let scale: CGFloat = 0.0 // Automatically use scale factor of main screen
UIGraphicsBeginImageContextWithOptions(size, !hasAlpha, scale)
image.drawInRect(CGRect(origin: CGPointZero, size: size))
let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return scaledImage
}
imageView.contentMode = .ScaleAspectFill
imageView.clipsToBounds = true;