我正在尝试用UIGraphicsBeginImageContext.创建一个拼贴图像但是裁剪一个图像的特定位置,并将imageView放在一个合适的位置上,这让我发疯了。我到目前为止的尝试:
-(void) viewWillAppear:(BOOL)animated
{
[self noOfImages:2]; // no of images method used for how many image you want to show in the view. Currently I needed a collage imageView upto 3 images
}
-(void)noOfImages:(int)num
{
if (num==2)
{
UIImageView *One=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, containerView.frame.size.width, containerView.frame.size.height)];
[One setBackgroundColor:[UIColor grayColor]];
[One setImage:[UIImage imageNamed:@"1.jpg"]];
UIImageView *Two=[[UIImageView alloc] initWithFrame:CGRectMake(containerView.frame.size.width/2, 0, containerView.frame.size.width/2, containerView.frame.size.height)];
[Two setBackgroundColor:[UIColor grayColor]];
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"jpg"];
UIImage *existingImage = [[UIImage alloc] initWithContentsOfFile:imagePath];
// Create new image context (retina safe)
UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 200), NO, [UIScreen mainScreen].scale);
// Create rect for image
CGRect rect = CGRectMake(1, 0, containerView.frame.size.width, containerView.frame.size.height);
// Draw the image into the rect
[existingImage drawInRect:rect];
// Saving the image, ending image context
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[Two setImage:newImage];
[containerView addSubview:One];
[containerView addSubview:Two];
}
else if (num==3)
{
UIImageView *One=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, containerView.frame.size.width, containerView.frame.size.height)];
[One setBackgroundColor:[UIColor grayColor]];
[One setImage:[UIImage imageNamed:@"1.jpg"]];
UIImageView *Two=[[UIImageView alloc] initWithFrame:CGRectMake(containerView.frame.size.width/2, 0, containerView.frame.size.width/2, containerView.frame.size.height/2)];
[Two setBackgroundColor:[UIColor grayColor]];
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"jpg"];
UIImage *existingImage = [[UIImage alloc] initWithContentsOfFile:imagePath];
// Create new image context (retina safe)
UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 200), NO, [UIScreen mainScreen].scale);
// Create rect for image
CGRect rect = CGRectMake(1, 0, containerView.frame.size.width, 200);
// Draw the image into the rect
[existingImage drawInRect:rect];
// Saving the image, ending image context
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[Two setImage:newImage];
UIImageView *Three=[[UIImageView alloc] initWithFrame:CGRectMake(containerView.frame.size.width/2, containerView.frame.size.height/2, containerView.frame.size.width/2, containerView.frame.size.height/2)];
[Three setBackgroundColor:[UIColor grayColor]];
NSString *imagePath3 = [[NSBundle mainBundle] pathForResource:@"3" ofType:@"jpg"];
UIImage *existingImage3 = [[UIImage alloc] initWithContentsOfFile:imagePath3];
// Create new image context (retina safe)
UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 200), NO, [UIScreen mainScreen].scale);
// Create rect for image
CGRect rect3 = CGRectMake(1, 0, containerView.frame.size.width, 200);
// Draw the image into the rect
[existingImage3 drawInRect:rect3];
// Saving the image, ending image context
UIImage * newImage3 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[Three setImage:newImage3];
[containerView addSubview:One];
[containerView addSubview:Two];
[containerView addSubview:Three];
}
}两个图像的输出应该是:

三幅图像的输出应该是:

实际上,当我使用200x200图像时,输出是correct.But --这段代码不能从我不知道高度和宽度的动态图像中运行。另外,右侧图像有点拉伸,对两个images.The右侧图像的位置不正确)x位置应该从中间点开始到端,而不是起点到中间。我知道我的计算错了。但无法使用UIGraphicsBeginImageContextWithOptions或添加为子视图的imageview设置适当的高度、宽度、位置或裁剪。
我需要输出类似:对于两个图像-第一个图像的一半在左边和另一个图像一半右。对于三张图片--第一张图片的一半在左边,第二张图像的一半在右上角,第三张图像的一半在右下角。
发布于 2015-05-22 17:05:50
按比例将第二张图像放大,然后将其定位到应有的位置。在项目中直接编写代码对您来说会更容易。
https://stackoverflow.com/questions/30399034
复制相似问题