我正在开发Pic Collage类型的应用程序。我有两个滚动视图,如下图所示。
我已使用此代码创建要保存在图库中的图像
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultingImage;
但是这会创建一张320 X 460分辨率图像的图像,这不适合在Facebook / Twitter上分享或添加个人资料或封面照片等。
这就是我尝试在用户点击Done时以编程方式创建所有内容的原因。我面临的唯一问题是我无法在此获得相同的缩放比例。当我以编程方式再次创建所有内容时,滚动视图应该被缩放,以便当我组合640 X 960分辨率图像的视图时,scrollview子视图,即图像也应该看起来像用户在滚动中设置的内容。这是代码:
UIView *mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 640, 960)];
[mainView setBackgroundColor:[UIColor grayColor]];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 20, 290, 920)];
[scrollView setBackgroundColor:[UIColor redColor]];
[mainView addSubview:scrollView];
UIScrollView *scrollView11 = [[UIScrollView alloc] initWithFrame:CGRectMake(330, 20, 290, 920)];
[scrollView11 setBackgroundColor:[UIColor redColor]];
[mainView addSubview:scrollView11];
[scrollView1.layer setBorderWidth:0.0];
[scrollView2.layer setBorderWidth:0.0];
[[[scrollView1 subviews] objectAtIndex:0] removeFromSuperview];
[[[scrollView2 subviews] objectAtIndex:0] removeFromSuperview];
[scrollView1 setFrame:CGRectMake(20, 20, 290, 920)];
[scrollView2 setFrame:CGRectMake(330, 20, 290, 920)];
UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 290, 100)];
[imgV setImage:[self resizeImage:[UIImage imageNamed:@"photo1.png"] width:1166 height:960]];
//[imgV sizeToFit];
[scrollView1 addSubview:imgV];
UIImageView *imgV1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 290, 100)];
[imgV1 setImage:[self resizeImage:[UIImage imageNamed:@"photo2.png"] width:846 height:960]];
//[imgV1 sizeToFit];
[scrollView2 addSubview:imgV1];
scrollView1.contentSize = imgV.image.size;
scrollView2.contentSize = imgV1.image.size;
NSLog(@"scrollview subviews - %@",[scrollView1 subviews]);
[self initScrollView:scrollView1];
[self initScrollView:scrollView2];
[self centerScrollViewContents:scrollView1 withImageView:imgV];
[self centerScrollViewContents:scrollView2 withImageView:imgV1];
CGFloat newZoomScale = scrollView1.zoomScale;
// Figure out the rect we want to zoom to, then zoom to it
CGSize scrollViewSize = scrollView1.bounds.size;
CGFloat w = scrollViewSize.width / newZoomScale;
CGFloat h = scrollViewSize.height / newZoomScale;
CGFloat x = point.x - (w / 2.0f);
CGFloat y = point.y - (h / 2.0f);
CGRect rectToZoomTo = CGRectMake(rect.origin.x*2, rect.origin.y*2, rect.size.width*2, rect.size.height*2);
CGRect rectToZoom = CGRectMake(x, y, w, h);
[scrollView1 zoomToRect:rectToZoom animated:YES];
newZoomScale = scrollView2.zoomScale;
// Figure out the rect we want to zoom to, then zoom to it
scrollViewSize = scrollView2.bounds.size;
w = scrollViewSize.width / newZoomScale;
h = scrollViewSize.height / newZoomScale;
x = point1.x - (w / 2.0f);
y = point1.y - (h / 2.0f);
rectToZoomTo = CGRectMake(rect1.origin.x*2, rect1.origin.y*2, rect1.size.width*2, rect1.size.height*2);
rectToZoom = CGRectMake(x, y, w, h);
[scrollView2 zoomToRect:rectToZoom animated:YES];
[mainView addSubview:scrollView1];
[mainView addSubview:scrollView2];
[scrollView1.layer setBorderWidth:0.0];
[scrollView2.layer setBorderWidth:0.0];
请帮忙。 在此先感谢!