我是iOS Swift开发的新手,但操作图像更新。我在UIScrollView中有一个UIImageView。我希望用户缩放/平移以显示他们想要捕获的确切图像。我有一点工作。当用户单击按钮时,它会正确捕获图像以及UIScrollView框中的确切内容,但是,图像的SIZE不正确。任何帮助将不胜感激!!
@IBAction func tapSendtoBand(sender: UIButton) {
var frameInfo = frameForImage(photoImageView.image!, imageView: photoImageView)
var imageWidth = photoImageView.image!.size.height
var scale:CGFloat = CGFloat(1.0) / (photoImageView.frame.size.height / imageWidth)
var visibleRect = CGRect()
var pWidth = CGFloat(CGImageGetWidth(photoImageView.image?.CGImage))
var pHeight = CGFloat(CGImageGetHeight(photoImageView.image?.CGImage))
visibleRect.origin = scrollView.contentOffset
visibleRect.size = scrollView.bounds.size
visibleRect.origin.x *= scale
visibleRect.origin.y *= scale
visibleRect.size.width *= scale
visibleRect.size.height *= scale
var pSize:CGSize = CGSize(width: visibleRect.size.width,height: visibleRect.size.height)
let bRect = AVMakeRectWithAspectRatioInsideRect(pSize, visibleRect)
UIImageWriteToSavedPhotosAlbum(cropImage(photoImageView.image!, rect: bRect), nil, nil, nil)
photoTest.image = cropImage(photoImageView.image!, rect: bRect)
}
这是我用来尝试确定大小调整的框架函数
func frameForImage (image:UIImage, imageView:UIImageView) -> CGRect{
var imageRatio = image.size.width / image.size.height;
var viewRatio = imageView.frame.size.width / imageView.frame.size.height;
if(imageRatio < viewRatio){
var scale = imageView.frame.size.height / image.size.height;
var width = scale * image.size.width;
var topLeftX = (imageView.frame.size.width - width) * 0.5;
return CGRectMake(topLeftX, 0, width, imageView.frame.size.height)
}
else{
var scale = imageView.frame.size.width / image.size.width;
var height = scale * image.size.height;
var topLeftY = (imageView.frame.size.height - height) * 0.5;
return CGRectMake(0, topLeftY, imageView.frame.size.width, height);
}
}
这是我用来实际裁剪图像的功能
func cropImage(srcImage:UIImage,rect:CGRect) -> UIImage
{
var cgImageConv = srcImage.CGImage
var cgSizeConv:CGSize = CGSize(width: 310,height: 102)
var cr:CGImageRef = CGImageCreateWithImageInRect(cgImageConv, rect)
var cropped:UIImage = UIImage(CGImage: cr)!
return cropped
}
答案 0 :(得分:0)
以下代码将图像视图的模式设置为宽高比并填充屏幕区域。
//Makes your image view fill the real estate you have available on your phones screen
yourUIImageView.autoresizingMask = UIViewAutoresizing.FlexibleBottomMargin | UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleRightMargin | UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleWidth
\
//Will make your image view Aspect ratio
yourUIImageView.contentMode = UIViewContentMode.ScaleAspectFit
答案 1 :(得分:0)
我实际上已经找到了问题所在。这是我的方法。使用我想要捕获的可见区域的代码,我能够使用以下代码以正确的比例获得我想要的确切大小。
var cr = CGImageCreateWithImageInRect(cgImageConv, visibleRect)
var cfUIImage = UIImage(CGImage: cr)
UIGraphicsBeginImageContext(CGSize(width: 620, height: 204))
var context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0.0, 102);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, CGRectMake(CGFloat(0.0), CGFloat(0.0), 310, 102), cr);
var scaledImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
photoTest.image = scaledImage
UIImageWriteToSavedPhotosAlbum(scaledImage, nil, nil, nil)
答案 2 :(得分:0)
关于previos帖子的Swift 3解决方案:
func frameForImageInImageViewAspectFit(imageView : UIImageView) -> CGRect {
let img = imageView.image
let frame = imageView.frame
let imageRatio = (img?.size.width)! / (img?.size.height)!;
let viewRatio = frame.size.width / frame.size.height;
if (imageRatio < viewRatio) {
let scale = frame.size.height / (img?.size.height)!;
let width = scale * (img?.size.width)!;
let topLeftX = (frame.size.width - width) * 0.5;
return CGRect(x: topLeftX, y: 0, width: width, height: frame.size.height)
}
else {
let scale = frame.size.width / (img?.size.width)!;
let height = scale * (img?.size.height)!;
let topLeftY = (frame.size.height - height) * 0.5;
return CGRect(x: 0, y: topLeftY, width: frame.size.width, height: height)
}
}
但更好的解决方案是(Swift 3):
self.imageView.frame = AVMakeRect(aspectRatio: (self.imageView.image?.size)!, insideRect: self.imageView.frame)