来自邮件中的 URL图像
我正在将图像添加到邮件视图。它将显示完整图像。但我想计算,按比例改变图像的高度和宽度。
我怎样才能得到的高度和宽度UIImage
?
let heightInPoints = image.size.height
let heightInPixels = heightInPoints * image.scale
let widthInPoints = image.size.width
let widthInPixels = widthInPoints * image.scale
Use the size
property on the UIImage instance. See the documentation for more details.
UIImage *img = [UIImage imageNamed:@"logo.png"];
CGFloat width = img.size.width;
CGFloat height = img.size.height;
上面的一些答案,在旋转设备时不能很好地工作。
尝试:
CGRect imageContent = self.myUIImage.bounds;
CGFloat imageWidth = imageContent.size.width;
CGFloat imageHeight = imageContent.size.height;
UIImageView *imageView = [[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"MyImage.png"]]autorelease];
NSLog(@"Size of my Image => %f, %f ", [[imageView image] size].width, [[imageView image] size].height) ;
有很多有用的解决方案,但没有简化的扩展方法。这是解决扩展问题的代码:
extension UIImage {
var getWidth: CGFloat {
get {
let width = self.size.width
return width
}
}
var getHeight: CGFloat {
get {
let height = self.size.height
return height
}
}
}
import func AVFoundation.AVMakeRect
let imageRect = AVMakeRect(aspectRatio: self.image!.size, insideRect: self.bounds)
x = imageRect.minX
y = imageRect.minY
let imageView: UIImageView = //this is your existing imageView
let imageViewHeight: CGFloat = imageView.frame.height
let imageViewWidth: CGFloat = imageView.frame.width