私のiPhoneアプリでは、画像編集を行う必要があります。ユーザーは、画像にキャプションを追加し、その画像をキャプション付きで保存して、その画像を共有できます。最初にギャラリーから画像を選択するとき、以下の方法を使用して、画像ビューの画像をスケーリングしました。
UIGraphicsBeginImageContextWithOptions(screenRect.size, NO, 0.0f);
float hfactor = img.size.width / screenRect.size.width;
float vfactor = img.size.height / screenRect.size.height;
float factor = MAX(hfactor, vfactor);
float newWidth = img.size.width / factor;
float newHeight = img.size.height / factor;
float leftOffset = (screenRect.size.width - newWidth) / 2;
float topOffset = (screenRect.size.height - newHeight) / 2;
if (leftOffset>0) {
mainImgView.frame=CGRectMake(leftOffset+10, screenRect.origin.y, newWidth, newHeight);
}
else if(topOffset>0)
{
mainImgView.frame=CGRectMake(screenRect.origin.x, topOffset+10, newWidth, newHeight);
}
CGRect newRect = CGRectMake(0, 0, screenRect.size.width, screenRect.size.height);
[img drawInRect:newRect blendMode:kCGBlendModePlusDarker alpha:1];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
キャプション付きの画像を保存するために、以下のコードを使用して、画像ビューとキャプション ラベルの両方が撮影されたビューのスクリーンショットを撮りました。
UIGraphicsBeginImageContextWithOptions(mainImgView.frame.size,NO,0.0f);
[mainImgView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *newImage=[[UIImage alloc]init];
newImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
ただし、画面を短くすると、保存される画像の解像度が非常に低くなり、その画像をズームするとピクセル化されます。画像とキャプションの両方が画像として保存されるように、実際の解像度で画像を保存する方法はありますか?