Рисуем UIImage с трансформациями в iOS - PullRequest
0 голосов
/ 27 февраля 2012

У меня есть несколько трансформируемых UIImageViews (украшает фотографию), которые пользователь может перетаскивать, сжимать и вращать. После редактирования я хочу, чтобы приложение генерировало окончательное изображение. Как я могу нарисовать UIImageViews с учетом их свойства transform.

Мое первое решение использует renderInContext, но оно может генерировать только изображение размером 320x480. Можно ли сгенерировать изображение с любым разрешением?

Тогда я использую код следующим образом:

UIGraphicsBeginImageContext(CGSizeMake(640.0f, 896.0f));
CGContextRef currentContext = UIGraphicsGetCurrentContext();

UIImage *backgroundImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"fittingBackground" ofType:@"jpg"]];
[backgroundImage drawInRect:CGRectMake(0.0f, 0.0f, 640.0f, 896.0f)];

CGContextSaveGState(currentContext);
CGContextConcatCTM(currentContext, self.photoImageView.transform);
CGRect photoFrame = self.photoImageView.frame;
[self.photoImageView.image drawInRect:CGRectMake(photoFrame.origin.x * 2, photoFrame.origin.y * 2, photoFrame.size.width * 2, photoFrame.size.height * 2)];
CGContextRestoreGState(currentContext);

CGContextSaveGState(currentContext);
CGContextConcatCTM(currentContext, self.productImageView.transform);
CGRect productFrame = self.productImageView.frame;
[self.productImageView.image drawInRect:CGRectMake(productFrame.origin.x * 2, productFrame.origin.y * 2, productFrame.size.width * 2, productFrame.size.height * 2)];
CGContextRestoreGState(currentContext);

UIImage *resultImage = [UIImage imageWithCGImage:CGBitmapContextCreateImage(currentContext)];

UIGraphicsEndImageContext();

Однако результат кажется странным. И я записал рамку, границы и центр. Я не знаю, какой Rect мне следует использовать в drawInRect после применения преобразования?

1 Ответ

0 голосов
/ 27 февраля 2012

Попробуйте это,

-(void)drawBackgroundInLayer:(CALayer*)destinationLayer {

    CGFloat scaledFactor = [[UIScreen mainScreen] scale];
    CGRect layerFrame = destinationLayer.frame;
    CGRect scaledRect = CGRectMake(layerFrame.origin.x, 
                                   layerFrame.origin.y, 
                                   (layerFrame.size.width)*scaledFactor, 
                                   (layerFrame.size.height)*scaledFactor);

    // Get the size and do some drawings
    CGSize sizeBack = CGSizeMake(layerFrame.size.width*scaledFactor, layerFrame.size.height*scaledFactor);
    UIGraphicsBeginImageContext(sizeBack);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetRGBFillColor(context, 0., 0., 0., 1.);
    drawBorder(context, scaledRect);

    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    if (destinationLayer) {
        [destinationLayer setContents:(id)img.CGImage];
    }
}

Наиболее важные строки - UIGraphicsBeginImageContext (sizeBack);UIImage * img = UIGraphicsGetImageFromCurrentImageContext ();

...