1
我的相機的自定義覆蓋圖僅在中心留下了320x320的正方形,因此用戶只能拍攝屏幕上的圖片。UIImagePickerController使用自定義覆蓋圖進行縮放和裁剪
我檢索原始圖片,然後我想縮小它並只裁剪疊加層上可見的區域。
我在使用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方形圖像給我的圖像矩形,我會很感激。