我已經在我的身邊做了很多工作。最後我需要幫助。感謝在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