ユーザーがカメラまたは写真ライブラリから画像を選択できるアプリがあります。次に、その画像を ImageView に配置します。次に、Quartz を使用してその画像ビューから画像を取得し、いくつかの基本的なオーバーレイなどと共にレンダリングします。
問題は、画像がカメラから、またはカメラ ロールから選択されたときに、Quartz が 90 度回転して画像を描画し、画像を垂直方向に引き伸ばすことです。フォト ライブラリから他の写真を選択しても問題ありません。また、allowsImageEditing が YES に設定されている場合、問題は発生しません。
誰かが光を当てることができれば、それは役に立ちます。私はこれで比較的初心者です。
私の CameraViewController のコード
#pragma mark -
- (IBAction)getCameraPicture {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.navigationBar.barStyle = UIBarStyleBlackOpaque;
picker.delegate = self;
picker.allowsImageEditing = NO; //seems to fix the weird rotation & stretching issues if set to YES
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
//[picker release];
}
- (IBAction)selectExistingPicture {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.navigationBar.barStyle = UIBarStyleBlackOpaque;
picker.delegate = self;
picker.allowsImageEditing = NO; //seems to fix the weird rotation & stretching issues if set to YES (only with camera roll)
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
//[picker release];
}
}
#pragma mark -
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo {
imageView.image = image;
NSLog(@"imagePicker image size %.0f x %.0f", image.size.width, image.size.height);
[picker dismissModalViewControllerAnimated:YES];
[picker release];
[quartzContextView setNeedsDisplay];
[self setUpAndRenderImage];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissModalViewControllerAnimated:YES];
[picker release];
}
Quartz を描画するビューのコード
NSLog(@"Quartz image size %.0f x %.0f", drawImageTwo.size.width, drawImageTwo.size.height);
if (drawImageTwo.size.width >= drawImageTwo.size.height)
{
scaleFactor = (insideHeight+5)/drawImageTwo.size.height;
xOffset = (drawImageTwo.size.width*scaleFactor - insideWidth)/2;
yOffset = 0;
NSLog(@"Image is Landscaepe");
}
else if (drawImageTwo.size.width < drawImageTwo.size.height)
{
scaleFactor = (insideWidth+5)/drawImageTwo.size.width;
yOffset = (drawImageTwo.size.height*scaleFactor - insideHeight)/2;
xOffset = 0;
NSLog(@"Image is Portrait");
}
//draw Photo Base
CGRect baseRect = CGRectMake(0.0, 0.0, width, height);
CGContextDrawImage (context, baseRect, drawImage.CGImage);
//clip all further drawing to context clip
CGContextClipToRect(context, CGRectMake(leftOffset, (height-insideHeight-topOffset), insideWidth, insideHeight));
//draw Photo
CGRect photoRect = CGRectMake(leftOffset-xOffset, (height-insideHeight-topOffset)-yOffset, drawImageTwo.size.width*scaleFactor, drawImageTwo.size.height*scaleFactor);
CGContextDrawImage (context, photoRect, drawImageTwo.CGImage);