@ Оле, мне кажется, я не могу найти коэффициент модификации для рисования. Я думал, что это было просто: я должен умножить положение и масштаб на соотношение (размер изображения / размер изображения). Как это не правильно?

аюсь визуализировать некоторые UIImages в одно изображение, которое я могу сохранить в своем фотоальбоме. Но похоже, что layer.renderInContext не учитывает маску слоя?

Текущее поведение: фотография сохраняется, и я вижу mosaicLayer без маскирующего эффекта maskLayer.

Ожидаемое поведение: фотография сохраняется, и я вижу изображение в моем представлении, поверх которого находится слой мозаики в маске.

Я использую следующий код для маскировки изображения

UIImage *maskImg = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]
                        pathForResource:@"mask" ofType:@"png"]];

    maskLayer = [[UIImageView alloc] initWithImage:maskImg];
    maskLayer.multipleTouchEnabled = YES;
    maskLayer.userInteractionEnabled = YES;
    UIImageView *mosaicLayer = [[UIImageView alloc] initWithImage:img];
    mosaicLayer.contentMode = UIViewContentModeScaleAspectFill;

    mosaicLayer.frame = [imageView bounds]; 
    mosaicLayer.layer.mask = maskLayer.layer;

    [imageView addSubview:mosaicLayer];

А затем я использую этот код для сохранения моего составного изображения:

UIGraphicsBeginImageContext(imageView.bounds.size);
    [imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *saver = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIImageWriteToSavedPhotosAlbum(saver, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

РЕДАКТИРОВАТЬ: Это правильно применяет маску

-(IBAction) saveImage { 
UIImage * saver = nil;
CGImageRef image = imageView.image.CGImage;

size_t cWidth = CGImageGetWidth(image);
size_t cHeight = CGImageGetHeight(image);
size_t bitsPerComponent = 8; 
size_t bytesPerRow = 4 * cWidth;

//Now we build a Context with those dimensions.
CGContextRef context = CGBitmapContextCreate(nil, cWidth, cHeight, bitsPerComponent, bytesPerRow, CGColorSpaceCreateDeviceRGB(), CGImageGetBitmapInfo(image));

//The location where you draw your image on the context is not always the same location you have in your UIView, 
//this could change and you need to calculate that position according to the scale between you images real size, and the size of the UIImage being show on the UIView. Hence the mod floats...
CGContextDrawImage(context, CGRectMake(0, 0, cWidth,cHeight), image);

float mod = cWidth/(imageView.frame.size.width);
float modTwo = cHeight/(imageView.frame.size.height);

//Make the drawing right with coordinate switch
CGContextTranslateCTM(context, 0, cHeight);
CGContextScaleCTM(context, 1.0, -1.0);

CGContextClipToMask(context, CGRectMake(maskLayer.frame.origin.x * mod, maskLayer.frame.origin.y * modTwo, maskLayer.frame.size.width * mod,maskLayer.frame.size.height * modTwo), maskLayer.image.CGImage);

//Reverse the coordinate switch
CGAffineTransform ctm = CGContextGetCTM(context);
ctm = CGAffineTransformInvert(ctm);
CGContextConcatCTM(context, ctm);

CGContextDrawImage(context, CGRectMake(0, 0, cWidth,cHeight), mosaicLayer.image.CGImage);

CGImageRef mergeResult  = CGBitmapContextCreateImage(context);
saver = [[UIImage alloc] initWithCGImage:mergeResult];
CGContextRelease(context);
CGImageRelease(mergeResult);

UIImageWriteToSavedPhotosAlbum(saver, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);}

Ответы на вопрос(1)

Ваш ответ на вопрос