UIImagePickerController使用自定义叠加进行缩放和裁剪

时间:2013-01-21 19:51:36

标签: iphone ios objective-c uiimage uiimagepickercontroller

我的相机的自定义叠加层仅在中心留下320x320的正方形,因此用户只能在屏幕上拍摄照片。

我检索原始图片然后我想缩小它并仅裁剪覆盖图上可见的区域。

我无法使用iPhone5进行操作。适用于iPhone3gs,4和4s。

这是我到目前为止使用的方法:

-(UIImage *)squareImageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    double ratio;
    double delta;
    CGPoint offset;

    //make a new square size, that is the resized imaged width
    CGSize sz = CGSizeMake(newSize.width, newSize.width);

    //figure out if the picture is landscape or portrait, then
    //calculate scale factor and offset
    if (image.size.width > image.size.height) {
        ratio = newSize.width / image.size.width;
        delta = (ratio*image.size.width - ratio*image.size.height);
        offset = CGPointMake(delta/2, 0);
    } else {
        ratio = newSize.width / image.size.height;
        delta = (ratio*image.size.height - ratio*image.size.width);
        if (self.isPickingFromLibrary) {
            offset = CGPointMake(0, delta/2);
        } else {
            // Add 25 for image offset
            offset = CGPointMake(0, delta/2+25);
        }
    }

    //make the final clipping rect based on the calculated values
    CGRect clipRect = CGRectMake(-offset.x, -offset.y,
                                 (ratio * image.size.width) + delta,
                                 (ratio * image.size.height) + delta);


    //start a new context, with scale factor 0.0 so retina displays get
    //high quality image
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
        UIGraphicsBeginImageContextWithOptions(sz, YES, 0.0);
    } else {
        UIGraphicsBeginImageContext(sz);
    }
    UIRectClip(clipRect);
    [image drawInRect:clipRect];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

在运行此方法后的iPhone5上,图像被裁剪并大幅缩放。

我正在尝试为方形图像制作一个不错的自定义相机,而且我已经用这个打了几个星期。我甚至考虑过使用AVFoundation,但我甚至不知道从哪里开始。

如果有人可以帮我缩放并裁剪uiimagepicker中的方形图像,对于图像上给出矩形的任何设备,我会很感激。

1 个答案:

答案 0 :(得分:0)

如果您的设备是视网膜显示器,那么您应该应用此代码来调整图像大小

-(BOOL)iPhoneRetina{
return ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] && ([UIScreen mainScreen].scale == 2.0))?1:0;

}

if([self iPhoneRetina]==TRUE){

    CGRect rect =  CGRectMake(0, 0, point.origin.x, self.trackBackground.frame.size.height);
    rect.size.height = rect.size.height * [_trackImage scale];
    rect.size.width = rect.size.width * [_trackImage scale];
    rect.origin.x = rect.origin.x * [_trackImage scale];
    rect.origin.y = rect.origin.y * [_trackImage scale];
    CGImageRef sourceImageRef = [_trackImage CGImage];
    CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect);
    cropped = [UIImage imageWithCGImage:newImageRef scale:[_trackImage scale] orientation:[_trackImage imageOrientation]];

}
else{
    imageRef = CGImageCreateWithImageInRect([_trackImage CGImage], CGRectMake(0, 0, point.origin.x, self.trackBackground.frame.size.height));
    cropped = [UIImage imageWithCGImage:imageRef];
}

其中rect是必须裁剪的图像矩形,然后根据视网膜调整大小。