Tôi đã làm rất nhiều nỗ lực từ phía tôi. cuối cùng tôi cần giúp đỡ. nhờcắt một hình ảnh được phóng to trong ImageView bên trong scrollView
Mục tiêu: 1) Làm thế nào tôi phù hợp với IMAGExem bên scrollview
2) Làm thế nào tôi cắt một hình ảnh thu nhỏ trong scrollview bên trong.
Tôi có một imageView bên trong Chế độ xem cuộn. Tôi muốn cắt hình ảnh sau khi phóng to được hiển thị bên trong ranh giới scrollview. Tôi đã cắt hình ảnh nhưng nó không chính xác giống như tôi muốn.
Ở đây tôi đặt backgroundColor Black thành scrollView của tôi. Và khi tôi đặt imageView bên trong nó, nó không phù hợp.
sau khi hình ảnh zoom bên trong xem di chuyển được
và sau khi hình ảnh cây trồng là
và mã của tôi là ở đây:
- (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;
}
thiết lập s xem Croll 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;
}
và logic cây trồng của tôi là
-(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;
}
Và hình ảnh cây trồng phương pháp này:
- (UIImage*)imageByCropping:(UIImage *)myImage toRect:(CGRect)cropToArea{
CGImageRef cropImageRef = CGImageCreateWithImageInRect(myImage.CGImage, cropToArea);
UIImage* cropped = [UIImage imageWithCGImage:cropImageRef];
CGImageRelease(cropImageRef);
return cropped;
}
Tôi đang làm rung chuyển điều này. Vấn đề duy nhất tôi tìm thấy với mã ở trên là trong mã bên dưới tiêu đề "// khung cắt ảnh trong tọa độ bù đắp hình ảnh". Bạn đang sử dụng self.cropview.frame trong khi tôi sử dụng self.cropview.bounds. –
Cool ..... cảm ơn dude..working awesome .. – Gokul