私のカメラのカスタムオーバーレイは、中央に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から正方形の画像を拡大縮小してトリミングするのを手伝ってくれるなら、私はそれをいただければ幸いです。