在下面的代码中,即使是小图像放大并填充整个视图。你能帮我看看如何显示图像的原始状态。
override func drawRect(rect: CGRect) {
if let inputCIImage = inputCIImage {
clampFilter.setValue(inputCIImage, forKey: kCIInputImageKey)
blurFilter.setValue(clampFilter.outputImage!, forKey: kCIInputImageKey)
let rect = CGRect(x: 0, y: 0, width: drawableWidth, height: drawableHeight)
ciContext.drawImage(blurFilter.outputImage!, inRect: rect, fromRect: inputCIImage.extent)
}
}
答案 0 :(得分:0)
我用以下方式想出来:
func aspectFit(fromRect: CGRect, toRect: CGRect) -> CGRect {
let fromAspectRatio = fromRect.size.width / fromRect.size.height;
let toAspectRatio = toRect.size.width / toRect.size.height;
var fitRect = toRect
if (fromAspectRatio > toAspectRatio) {
fitRect.size.height = toRect.size.width / fromAspectRatio;
fitRect.origin.y += (toRect.size.height - fitRect.size.height) * 0.5;
} else {
fitRect.size.width = toRect.size.height * fromAspectRatio;
fitRect.origin.x += (toRect.size.width - fitRect.size.width) * 0.5;
}
return CGRectIntegral(fitRect)
}
func aspectFill(fromRect: CGRect, toRect: CGRect) -> CGRect {
let fromAspectRatio = fromRect.size.width / fromRect.size.height;
let toAspectRatio = toRect.size.width / toRect.size.height;
var fitRect = toRect
if (fromAspectRatio > toAspectRatio) {
fitRect.size.width = toRect.size.height * fromAspectRatio;
fitRect.origin.x += (toRect.size.width - fitRect.size.width) * 0.5;
} else {
fitRect.size.height = toRect.size.width / fromAspectRatio;
fitRect.origin.y += (toRect.size.height - fitRect.size.height) * 0.5;
}
return CGRectIntegral(fitRect)
}
func imageBoundsForContentMode(fromRect: CGRect, toRect: CGRect) -> CGRect {
switch contentMode {
case .ScaleAspectFill:
return aspectFill(fromRect, toRect: toRect)
case .ScaleAspectFit:
return aspectFit(fromRect, toRect: toRect)
default:
return fromRect
}
}
override func drawRect(rect: CGRect) {
if let inputCIImage = inputCIImage {
clampFilter.setValue(inputCIImage, forKey: kCIInputImageKey)
blurFilter.setValue(clampFilter.outputImage!, forKey: kCIInputImageKey)
let rect = CGRect(x: 0, y: 0, width: drawableWidth, height: drawableHeight)
let inputBounds = inputCIImage.extent
let targetBounds = imageBoundsForContentMode(inputBounds, toRect: rect)
ciContext.drawImage(blurFilter.outputImage!, inRect: targetBounds, fromRect: inputCIImage.extent)
}
}
答案 1 :(得分:0)
您可以获得图像的大小,就像您的图像是
一样let image = UIImage(named: "someImage.png")
然后你可以得到它的宽度和大小
print(image.size)
然后将大小传递给自定义视图以制作所需的大小。