【问题标题】:Cropping an Image in iOS App在 iOS 应用中裁剪图像
【发布时间】:2013-05-25 13:17:30
【问题描述】:

我有一个图像视图,显示从相机或画廊拍摄的宽高比匹配图像。在 Imageview 的顶部,我放置了一个 UIView(可移动、可缩放)。

我想相对于 UIView 的框架裁剪图像区域。

所以我这样做了:

CGImageRef imageRef = CGImageCreateWithImageInRect([myImageView.image CGImage], cropV.frame);// cropV is the UIview over the myImageView

UIImage *img = [UIImage imageWithCGImage:imageRef];

CGImageRelease(imageRef);

问题是当我这样做时,我的矩形(UIview)在图像上的实际位置(尺寸很大)是不一样的。

例如。 对于 1024x1024 图像,图像 a 在 320x480 屏幕上的宽高比调整模式下显示不同 如果我的cropV在框架上(10,10,300,300); CGImageCreateWithImageInRect 将根据 1024x1024 的图像创建图像。

有没有办法可以根据图像大小缩放cropV 帧?这样矩形(cropV)的位置只是被裁剪的那部分。

现在我在上下文中绘制图像。但这会给宽屏/优质高清照片带来模糊的裁剪。

UIGraphicsBeginImageContext(myImageView.frame.size);

[myImageView.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], cropV.frame); 

【问题讨论】:

    标签: iphone ios crop


    【解决方案1】:

    您可以代表您的图像和视图获取比例,然后制作您的矩形。

    float ratio = yourImage.size.height/yourView.frame.size.height;  
    

    试试这个

    wRatio_width = myImageView.image.size.width/cropV.frame.size.width;
    wRatio_height = myImageView.image.size.height/cropV.frame.size.height;
    CGRect headImageRect = cropV.frame;
    headImageRect.origin.x=wRatio_width * headImageRect.origin.x;
    headImageRect.origin.y=wRatio_height * headImageRect.origin.y;
    
    headImageRect.size.width=wRatio_width * headImageRect.size.width;
    headImageRect.size.height=wRatio_height * headImageRect.size.height;
    
    cropV.frame=headImageRect;
    

    【讨论】:

    • 无法弄清楚如何在我的上下文中使用它!您是否建议计算 x、y、宽度和高度的比率,然后将我的cropV 帧与这些值相乘??
    • 获取关于你的图像(不是 UIImageView)和你的视图的配给。
    • 这样做了,但我得到的是完整图像而不是所需的裁剪区域。顺便说一句,这就是我所做的,'wRatio = myImageView.image/cropV.frame.size.width;'然后将我的cropV宽乘以比率。身高也一样。
    • 试试这个 wRatio_width = myImageView.image.size.width/cropV.frame.size.width; wRatio_height = myImageView.image.size.height/cropV.frame.size.height; CGRect headImageRect =cropV.frame; headImageRect.origin.x=wRatio_width * headImageRect.origin.x; headImageRect.origin.y=wRatio_height * headImageRect.origin.y; headImageRect.size.width=wRatio_width * headImageRect.size.width; headImageRect.size.height=wRatio_height * headImageRect.size.height; cropV.frame=headImageRect;
    • 你必须根据你的要求进行计算..我告诉你方法..现在由你决定..
    猜你喜欢
    • 2012-03-20
    • 2012-08-10
    • 2016-06-16
    • 2011-10-28
    • 2012-04-22
    • 2015-02-02
    • 1970-01-01
    • 2012-12-12
    • 2014-01-31
    相关资源
    最近更新 更多