我想根据红色 View 裁剪 Image 。有几点要记住。
1.图像可以滚动和缩放。2.Red ImageView是根据Image动态创建的
UIImage* whole = [UIImage imageNamed:@"9.png"]; //I uses this image
CGImageRef cgImg = CGImageCreateWithImageInRect(whole.CGImage, CGRectMake(x, y, incX, incY));
UIImage* part = [UIImage imageWithCGImage:cgImg];
我只想知道如何找到
x, y, incX, incY
谢谢...
场景一:正常(不滚动)
预期结果(忽略顶部和底部的黑色边框)
场景二:滚动
预期结果(忽略顶部和底部的黑色边框)
场景 3:放大
放大后的预期结果相同。
在所有情况下,我都希望红色矩形内的相应图像。
对于所有这些,我正在使用此代码...
-(void)cropClicked:(UIButton*)sender
{
float zoomScale = 1.0 / [mainScrollView zoomScale];
CGRect rect;
rect.size.width = [redImageView bounds].size.width * zoomScale ;
rect.size.height = [redImageView bounds].size.height * zoomScale ;
rect.origin.x = ([mainScrollView bounds].origin.x + redImageView.frame.origin.x );
rect.origin.y = ([mainScrollView bounds].origin.y + redImageView.frame.origin.y );
CGImageRef cr = CGImageCreateWithImageInRect([[mainImageView image] CGImage], rect);
UIImage *cropped = [UIImage imageWithCGImage:cr];
mainImageView.image=cropped;
UIImageWriteToSavedPhotosAlbum(cropped, nil, nil, nil);
CGImageRelease(cr);
}