如何在Xcode中裁剪缩放的图像? [英] How to crop zoomed image in xcode?

查看:66
本文介绍了如何在Xcode中裁剪缩放的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了图像缩放.现在我要裁剪缩放的图像.
有可能这样做吗?
我使用下面的代码缩放图像.

I have done zooming of an image. now I want to crop that zoomed image.
Is it possible to do that??
I used below code for zooming an image.

//Scroll View settings for Zoomin-ZoomOut
Scroll.contentSize= CGSizeMake(img.frame.size.width, img.frame.size.height);
Scroll.maximumZoomScale=3.0;
Scroll.minimumZoomScale=.20;
Scroll.clipsToBounds=YES;
Scroll.delegate=self;
[Scroll addSubview:img];
Scroll.imageContainer=img;


- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{       
    return img; 
}

这里我使用了customScroll类.

Here I have used customScroll Class.

推荐答案

如果要在滚动视图中缩放图像并想裁剪可见区域,请尝试以下操作:

If you are zooming an image in scroll view and wanna crop visible area try this:

-(IBAction) cropSelecteArea
{
//Calculate the required area from the scrollview
CGRect visibleRect;
float scale = 1.0f/scrollView.zoomScale;
visibleRect.origin.x = scrollView.contentOffset.x * scale;
visibleRect.origin.y = scrollView.contentOffset.y * scale;
visibleRect.size.width = scrollView.bounds.size.width * scale;
visibleRect.size.height = scrollView.bounds.size.height * scale;
UIImage *image = [self imageByCropping:previewImage toRect:visibleRect];
//UIImage *image = imageFromView(imageView.image, &visibleRect);
[croppedImageImageView setImage:self.objPostPikUploadPhotoPage.croppedImage];
}


- (UIImage*)imageByCropping:(UIImage *)myImage toRect:(CGRect)cropToArea{
CGImageRef cropImageRef = CGImageCreateWithImageInRect(myImage.CGImage, cropToArea);
UIImage* cropped = [UIImage imageWithCGImage:cropImageRef];

CGImageRelease(cropImageRef);
return cropped;
}

这篇关于如何在Xcode中裁剪缩放的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆