Я пытаюсь создать изображение коллажа с помощью UIGraphicsBeginImageContext. Но обрезание определенной позиции изображения и размещение imageView в правильном положении в представлении, заставляющем меня гайки. Моя попытка до сих пор:Расчет размера и положения изображения с использованием UIGraphicsBeginImageContext & drawRect для коллажа imageView в ios
-(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, выходы правильны. Но этот код не работает с динамическими изображениями, высота которых & ширина неизвестна мне. Также изображение правой стороны немного растянуто, а не в правильном положении для буксирных изображений. Правое изображение). X позиция должна начинаться от средней точки до конца, а не от начальной точки до середины. Я знаю, что мои вычисления ошибочны. Но не может установить правильную высоту, ширину, положение или обрезку с помощью UIGraphicsBeginImageContextWithOptions или imageview, который добавляется как подзаголовок.
Мне нужен выход, как: для двух изображений: первое изображение слева и другое изображение справа. для трех изображений - первое изображение - половина слева и вторая половина изображения в правом верхнем углу и в третьем изображении справа внизу.
Ваш вопрос непонятен. Нам нужно более точно узнать, как вы хотите обрезать и размещать изображения. Вы показывали изображения, но мы не знаем, как вы хотите, чтобы они выглядели, и что не так с изображениями, которые вы показываете. – rdelmar