UIImage 调整大小后居中
iPhone应用程序的代码:
+(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 sz = CGSizeMake( 200, 20 );
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image = [Class imageWithImage: [UIImage imageNamed:@"image.png"] scaledToSize: sz];
调整大小的图像在imageView中居中,我希望它左对齐(最好是左上角)。我怎样才能实现这个目标?感谢您的任何帮助。
Code for iPhone app:
+(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 sz = CGSizeMake( 200, 20 );
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image = [Class imageWithImage: [UIImage imageNamed:@"image.png"] scaledToSize: sz];
The resized image is centred in imageView, I would like it to be left aligned (preferably top-left). How can I achieve this? Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您将 imageView 设置为内容模式
UIViewContentModeScaleAspectFit
,iOS 将适合您的图像,但如果适合的宽度小于您的宽度,它也会在左侧和右侧引入透明条。解决方案是让iOS适应图像,然后计算出新的宽度并用新的宽度设置imageView的宽度。
If you have set your imageView to have the content mode
UIViewContentModeScaleAspectFit
, iOS will fit your image but it will also introduce transparent bars to the left and right if the fit is smaller than your width.The solution is to let iOS fit the image, then figure out the new width and set the imageView's width with the new width.
将内容模式更改为:
Change the contentMode to: