1

我的相机的自定义叠加层仅在中心留下一个 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 缩放和裁剪正方形图像,以用于在图像上给出矩形的任何设备,我将不胜感激。

4

1 回答 1

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 是图像的矩形,它必须被裁剪,然后根据视网膜调整大小。

于 2013-11-26T08:07:41.247 回答