在scrollView中的ImageView中裁剪缩放图像 [英] crop a zoomed image in ImageView inside scrollView
问题描述
我已经从我身边做了很多努力。最后我需要帮助。谢谢
I already do Lot of efforts from my Side. finally I need help. thanks
目标:
1)我如何在ScrollView中使用imageView
Goal : 1) How I fit imageView inside ScrollView
2)我如何裁剪内部scrollView中的缩放图像。
2) How I crop a zoomed Image in inside scrollView.
我在Scroll View中有一个imageView。我希望缩放后的裁剪图像显示在scrollview边界内。我已经裁剪了图像,但它与我想要的完全相同。
I have a imageView inside Scroll View. I wants crop image after zoomed which is display inside scrollview boundary. I cropped image already but it not exactly same which i wants.
这里我将backgroundColor Black设置为我的scrollView。当我在其中放置imageView时,它不适合。
Here I set backgroundColor Black to my scrollView. And when I place imageView inside it, it's not fit.
和裁剪图像后
我的代码在这里:
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame1 = CGRectMake(50,50,200,200);
CGRect frame2 = CGRectMake(50,50,200,200);
imageView1=[[UIImageView alloc]initWithFrame:frame1];
imageView1.image= [UIImage imageNamed:@"back.jpeg"];
imageView1.backgroundColor=[UIColor brownColor];
imageView1.contentMode = UIViewContentModeScaleAspectFit;
scroll=[[UIScrollView alloc]initWithFrame:frame2];
scroll.backgroundColor=[UIColor blackColor];
[scroll addSubview:imageView1];
scroll.delegate=self;
[self.view addSubview:scroll];
[self setContentSizeForScrollView];
self.imageView1.userInteractionEnabled = YES;
}
设置滚动视图contentSize
setting scroll view contentSize
-(void)setContentSizeForScrollView
{
// scroll.contentSize = CGSizeMake(imageView1.frame.size.width,imageView1.frame.size.height);
scroll.contentSize = CGSizeMake(200, 200);
scroll.minimumZoomScale = .50;
scroll.maximumZoomScale = 1.5;
}
我的裁剪逻辑是
-(IBAction)cropButtonClicked
{
//Calculate the required area from the scrollview
CGRect rect = CGRectMake(50, 50, 200,200);
UIImage *image = [self imageByCropping:imageView1.image toRect:rect];
imageView1.image=image;
imageView1.contentMode = UIViewContentModeScaleAspectFit;
}
此方法裁剪图片:
- (UIImage*)imageByCropping:(UIImage *)myImage toRect:(CGRect)cropToArea{
CGImageRef cropImageRef = CGImageCreateWithImageInRect(myImage.CGImage, cropToArea);
UIImage* cropped = [UIImage imageWithCGImage:cropImageRef];
CGImageRelease(cropImageRef);
return cropped;
}
推荐答案
我的回答问题:经过多方努力,我找到了我的两个问题的答案。
Answer of my own Question : After many efforts i found the answer of both of my questions.
这对我有用。我在这里分享,可能对某人有帮助。 :)
and it work good for me. I share here, may be it help someone. :)
1)在Scroll View中调整图像视图。我使用这个链接
1) Fit image View inside Scroll View. I use this link
- (void)centerScrollViewContents {
CGSize boundsSize = scroll.bounds.size;
CGRect contentsFrame = self.imageView1.frame;
if (contentsFrame.size.width < boundsSize.width) {
contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
}
else {
contentsFrame.origin.x = 0.0f;
}
if (contentsFrame.size.height < boundsSize.height) {
contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
}
else {
contentsFrame.origin.y = 0.0f;
}
self.imageView1.frame = contentsFrame;
}
2)在内部scrollView中裁剪缩放的图像。我使用这个链接
2) Crop a zoomed Image in inside scrollView. I use this link
UIGraphicsBeginImageContext(CGSizeMake(200, 200));
[scroll.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *fullScreenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imageView1.contentMode = UIViewContentModeScaleAspectFill;
UIImageWriteToSavedPhotosAlbum(fullScreenshot, nil, nil, nil);
return fullScreenshot;
这篇关于在scrollView中的ImageView中裁剪缩放图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!