2

画像のトリミング機能を備えた iPhone アプリを作成しています。ここでは、UIImagePickerController から写真を取得し、トリミングのために渡します。そこにはスクロールビューがあり、選択した画像がサブビューとしてスクロールビューに追加されます。そして、トリミングする領域を選択するために UIButton を使用しています。ユーザーはボタンを画像ビュー上に移動して任意の場所に配置できます。CROP ボタンをクリックすると、ボタンのフレーム サイズに似た領域が画像ビューからトリミングされます。

次のコードを使用しましたが、実際の画像が返されません。

CGRect clippedRect  = CGRectMake(self.scrollView.frame.origin.x+90, self.scrollView.frame.origin.y, self.scrollView.frame.size.width-180, self.scrollView.frame.size.height-220);
CGImageRef imageRef = CGImageCreateWithImageInRect([self.myPhoto CGImage], clippedRect);
UIImage *newImage   = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);

self.imageView.image = newImage;

も使用

- (UIImage *)cropImage:(UIImage *)oldImage {
    CGSize imageSize = self.cropFrame.frame.size;
    UIGraphicsBeginImageContextWithOptions( CGSizeMake( imageSize.width, imageSize.height), NO, 0.);
    [oldImage drawAtPoint:CGPointMake( xPosition, yPosition)
                blendMode:kCGBlendModeCopy
                    alpha:1.];
    UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return croppedImage;
}

しかし、結果の画像はボタン フレームのとおりの正確な画像ではありません。別のエリアから画像を取得しています。

更新されたコード

- (void)loadPhoto{

    CGFloat w = self.myPhoto.size.width;
    CGFloat h = self.myPhoto.size.height;
    CGRect imageViewFrame = CGRectMake(0.0f, 0.0f, roundf(w / 2.0f), roundf(h / 2.0f));
    self.scrollView.contentSize = imageViewFrame.size;

    UIImageView *iv = [[UIImageView alloc] initWithFrame:imageViewFrame];
    iv.contentMode = UIViewContentModeScaleAspectFit;
    iv.image = self.myPhoto;
    [self.view addSubview:iv];
    self.imageView = iv;
    [iv release];    
}

CGRect crop;//= CGRectMake(10, 10, 360, 360);
crop.origin.x = self.cropFrame.frame.origin.x;
crop.origin.y = self.cropFrame.frame.origin.y;
crop.size.width = roundf(self.cropFrame.frame.size.width * 2.0f); //self.cropFrame.frame.size.width * 2;
crop.size.height = roundf(self.cropFrame.frame.size.height * 2.0f);  //self.cropFrame.frame.size.height * 2;

NSLog(@"Rect: %@", NSStringFromCGRect(crop));
self.imageView.image = [self croppedImage:crop];

- (UIImage *)croppedImage:(CGRect)bounds {
    CGImageRef imageRef = CGImageCreateWithImageInRect([self.imageView.image CGImage], bounds);
    UIImage *croppedImage = [UIImage imageWithCGImage:imageRef scale:1.0 orientation:self.myPhoto.imageOrientation];
    CGImageRelease(imageRef);
    return croppedImage;
}

解決策を見つけるのを手伝ってください。

4

4 に答える 4

0

画像をスクロールできるscrollViewを使用しているため、croprectをscrollViewの位置に調整する必要があります。

float zoomScale = self.scrollView.zoomScale;
int cropX = (self.scrollView.contentOffset.x-imageView.frame.origin.x)/zoomScale;
int cropY = (self.scrollView.contentOffset.y-imageView.frame.origin.y)/zoomScale;
于 2012-12-27T10:42:53.797 に答える
0

私が作ったこのトリミングツールを使用できます。基本的に、ユーザーがトリミング領域を選択できるようにするインターフェイスを提供します。求めているものと一致していると思います。

https://github.com/nicholjs/BFCropInterface

于 2013-02-28T19:45:52.613 に答える
0

iOS には、画像をトリミングするためのデフォルト機能があります。このコードを試してください。

    picker.allowsEditing = YES;

また、このコントローラーでトリミングを確認してください。これはまさにあなたが探しているものですhttps://github.com/barrettj/BJImageCropper .

于 2012-12-27T07:40:46.707 に答える
0

あなたはこの問題を解決したと信じています。私もトリミング機能を試したときにこれを持っていました

imageView.size & scrollView.contentSize として image.size を設定します。以下のコードは、クロップする四角形を提供します

cropRect.origin = scrollView.contentOffset;
cropRect.size = scrollView.bounds.size;    
cropRect.origin.x /= scrollView.zoomScale;
cropRect.origin.y /= scrollView.zoomScale;
cropRect.size.width /= scrollView.zoomScale;
cropRect.size.height /= scrollView.zoomScale;

可視四角形で最初に完全な画像を表示する場合。imageView.size と scrollView.contentSize を表示可能なビュー サイズに設定すると、他の領域の画像がトリミングされます。代わりに、ズームスケールを見つけてみてください

CGFloat dxWidth = viewCrop.frame.size.width / imageView.image.size.width;
CGFloat dxHeight = viewCrop.frame.size.height / imageView.image.size.height;
CGFloat zoomScale = fmaxf(dWidth, dHeight)

そして適用します(subViewを追加する場合は、addSubViewの後)

scrollView.minimumZoomScale = zoomScale; // to disable further zoom-out
[scrollView setZoomScale: zoomScale];
于 2017-06-02T15:46:11.683 に答える