インスタグラムなどのアプリで正方形の写真を撮りたい。しかし、私はそれを行う方法がわかりません。y、画像はまだトリミングする必要があり、カメラは常に画像全体をキャプチャしていることを知っています。ビューで「ライブ」でトリミングしているだけです。画像がキャプチャされると、それはまだ画像全体であり、トリミングする必要があります。すべてバックグラウンドで行われるため、ユーザーだけがそれを認識していません。デバイスのカメラで画像をキャプチャしたら、それを UIImage のインスタンスに配置し、その UIIMage を取得して CGRefImageContext を使用してサイズを変更するメソッドを記述し、元の UIImage インスタンスが不要になった場合は元の UIImage インスタンスを上書きします。これを行う方法がわかりません。これを行う方法を示すサイトはありますか?またはそれを行う簡単な方法があります。
1 に答える
First, you need to take the photo with the camera, so add this code where you want to take the picture.
UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
pickerController.delegate = self;
[self presentViewController:pickerController
animated:YES
completion:nil];
}
Then, you need to implement delegate methods. Your class must conform to UIImagePickerControllerDelegate
, documented here. In the imagePickerController:
didFinishPickingMediaWithInfo:
method, you can take the taken photo in a UIImage
with:
UIImage *imageTaked = info[UIImagePickerControllerOriginalImage];
If you want, at this point you can take the metadata of the taken photo with
info [UIImagePickerControllerMediaMetadata];
Finally, when you have your photo in a UIImage, you can crop the image as follows:
CGFloat x, y, side; //We will see how to calculate those values later.
UIImage* imageCropped;
CGRect cropRect = CGRectMake(x,y,side,side);
CGImageRef imageRef = CGImageCreateWithImageInRect([imageTaked CGImage], cropRect);
imageCropped = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
So, the important thing here is how to calculate the cropRect
to cut the centre square of the UIImage. To do that, you can calculate x
, y
and side
as follows:
side = MIN(imageTaked.size.width, imageTaked.size.height
x = imageTaked.size.width / 2 - side / 2;
y = imageTaked.size.height / 2 - side / 2;
Ok, so, to summarise, all the code inside the imagePickerController:
didFinishPickingMediaWithInfo:
method results in:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *imageTaked = info[UIImagePickerControllerOriginalImage];
//You can take the metadata here => info [UIImagePickerControllerMediaMetadata];
UIImage* imageCropped;
CGFloat side = MIN(imageTaked.size.width, imageTaked.size.height);
CGFloat x = imageTaked.size.width / 2 - side / 2;
CGFloat y = imageTaked.size.height / 2 - side / 2;
CGRect cropRect = CGRectMake(x,y,side,side);
CGImageRef imageRef = CGImageCreateWithImageInRect([imageTaked CGImage], cropRect);
imageCropped = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
}
EDIT
If you want to send your photo to another device, probably the photo will be rotated. That's because Apple uses exif metadata for images, but other platforms don't. To solve this problem, just use this method to set appropriate metadata info:
-(UIImage*)getImageWithCorrectFiltersFromOriginalImage:(UIImage *)originalImage
{
UIImageOrientation orientation = UIImageOrientationUp;
NSNumber* orientationValue = [originalImage valueForProperty:@"ALAssetPropertyOrientation"];
if (orientationValue) {
orientation = [orientationValue intValue];
}
ALAssetRepresentation *assetRep = [originalImage defaultRepresentation];
CGImageRef imageRef = [assetRep fullResolutionImage];
return [UIImage imageWithCGImage:imageRef scale:[assetRep scale] orientation:orientation];
}
Hope it helps! and let me know if something goes wrong during copy-paste process, it's 2:00AM here, and i'm a zombie man right now!