我已经在我的身边做了很多工作。最后我需要帮助。感谢在ImageView里面滚动缩放图像scrollView
目标: 1)如何装进滚动型
2)如何在里面滚动视图裁剪缩放图像的ImageView。
我有一个imageView里面的滚动视图。我想要缩放后显示在滚动视图边界内的裁剪图像。我已经裁剪了图像,但它不完全一样,我想要的。
在这里,我设置backgroundColor黑色到我的scrollView。而当我把imageView放在它里面时,它不适合。
后滚动视图内缩放图像
后作物图像
,我的代码是在这里:
- (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;
}
设定S1克罗尔视图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;
}
我在摇这个。我在上面的代码中发现的唯一问题是在标题“//图像偏移坐标中的裁剪框架”下方的代码中。你正在使用self.cropview.frame,而我使用self.cropview.bounds。 – 2016-01-27 05:43:13
酷.....谢谢老兄..工作真棒.. – Gokul 2016-09-23 15:26:15