-3

我使用 CGContextDrawImage 渲染了几个 UIImage 对象。但是当我对图像应用旋转时,它没有应用并且视图消失了。 code here

-(void)renderImage: (ItemView *)array {

 NSArray *selectedImages =  self.slideView.selectedView.subviews;

CGSize combinedSize = CGSizeMake(0, 0);
for (int i = 0; i < [selectedImages count]; ++i) {
    CGSize sourceSize = [selectedImages[i] size];
    NSLog(@"sdfsd %f %f ", sourceSize.width, sourceSize.height);
    combinedSize.width = MAX(combinedSize.width, sourceSize.width);
    combinedSize.height += sourceSize.height;
}



   UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
{


    CGContextTranslateCTM(context, 0, 768);
    CGContextScaleCTM(context, 1,-1);




    for (int i = 0; i < [selectedImages count]; ++i) {
        UIImageView *imageview = selectedImages[i];
        UIImage *sourceImage = imageview.image;


        CGContextSaveGState(context);

        float radians1 = atan2(imageview.transform.a, imageview.transform.b);




       CGFloat angle = [(NSNumber *)[imageview valueForKeyPath:@"layer.transform.rotation.z"] floatValue];

        printf("\n radians %f",radians1);
        printf("\n angle %f",angle);

        CGContextRotateCTM(context, radians(angle));
        CGContextDrawImage(context, imageview.frame, imageview.image.CGImage);
        CGContextRestoreGState(context);

        //CGContextDrawImage(context, imageview.frame, imageview.image);

    }

}
UIImage *combinedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(combinedImage, nil, nil, nil);

}

谢谢,

任何帮助,将不胜感激

4

2 回答 2

0

假设您有一组源图像:

NSArray *sourceImages = ...;
// each element of sourceImages is a UIImage

现在你想count从数组中取出第一张图像并将它们垂直连接成一个新图像。首先计算组合图像的大小:

CGSize combinedSize = CGSizeMake(0, 0);
for (int i = 0; i < count; ++i) {
    CGSize sourceSize = [sourceImages[i] size];
    combinedSize.width = MAX(combinedSize.width, sourceSize.width);
    combinedSize.height += sourceSize.height;
}

接下来,创建该大小的图形上下文:

UIGraphicsBeginImageContextWithOptions(combinedSize, NO, 0); {

现在将每个组件图像绘制到适当位置的图形上下文中:

    CGFloat y = 0;
    for (int i = 0; i < count; ++i) {
        UIImage *sourceImage = sourceImages[i];
        [sourceImage drawAtPoint:CGPointMake(0, y)];
        y += sourceImage.size.height;
    }

最后,从上下文创建组合图像并处理上下文:

}
UIImage *combinedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

阅读iOS 绘图和打印指南- 绘图和创建图像了解更多信息。

于 2013-02-18T06:39:59.363 回答
0

我破解了答案。@rob mayoff:非常感谢帮助我的人。

 code here

//渲染图像组的代码,不包括在特定视图中选择的图像

-(void)renderImage: (ItemView *)selectedItem {

NSArray *selectedImages =  self.slideView.selectedView.subviews;
int selectedItemIndex = [selectedImages indexOfObject:selectedItem];
if(selectedItem.image==nil){
    [selectedItem loadImageFromFile];
}


// first set
UIGraphicsBeginImageContextWithOptions(self.slideView.selectedView.frame.size, NO, 0);
for (int i = 0; i < selectedItemIndex; ++i) {

    if([selectedImages[i] isKindOfClass:[ItemView class]]){

        ItemView *imageview = (ItemView *)selectedImages[i];
        CGAffineTransform transform = imageview.transform;
        imageview.transform = CGAffineTransformIdentity;

        UIImage *image = [[UIImage alloc] initWithContentsOfFile:imageview.filename];
        UIImageView *renderImageView = [[UIImageView alloc] initWithImage:image]; //[imageview copy];
        renderImageView.frame = imageview.frame;
        renderImageView.bounds = imageview.bounds;
        imageview.transform = transform;
        renderImageView.transform = transform;
        renderImageView.center = CGPointMake(imageview.frame.size.width/2,imageview.frame.size.height/2);
        UIView *view = [[UIView alloc] initWithFrame:renderImageView.frame];
        view.backgroundColor = [UIColor clearColor];
        [view addSubview:renderImageView];
        [renderImageView release];
        [image release];
        imageview.image = nil;

        UIGraphicsBeginImageContext(imageview.frame.size);
        CGContextRef context1 = UIGraphicsGetCurrentContext();
        [view.layer renderInContext:context1];
        UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        [view release];

        [result drawAtPoint:imageview.frame.origin];
    }

}
UIImage *combinedImage1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//UIImageWriteToSavedPhotosAlbum(combinedImage1, nil, nil, nil);


if (groupBgImage1) {
    [groupBgImage1 removeFromSuperview];
    [groupBgImage1 release];
    groupBgImage1 = nil;
}
groupBgImage1 = [[UIImageView alloc] initWithImage:combinedImage1];
[self.slideView.selectedView insertSubview:groupBgImage1 belowSubview:selectedItem ];

// second set
UIGraphicsBeginImageContextWithOptions(self.slideView.selectedView.frame.size, NO, 0);
for (int i = selectedItemIndex+1; i < [selectedImages count]; ++i) {


    if([selectedImages[i] isKindOfClass:[ItemView class]]){
        ItemView *imageview = (ItemView *)selectedImages[i];
        CGAffineTransform transform = imageview.transform;
        imageview.transform = CGAffineTransformIdentity;

        UIImage *image = [[UIImage alloc] initWithContentsOfFile:imageview.filename];
        UIImageView *renderImageView = [[UIImageView alloc] initWithImage:image]; //[imageview copy];
        renderImageView.frame = imageview.frame;
        renderImageView.bounds = imageview.bounds;
        imageview.transform = transform;
        renderImageView.transform = transform;
        renderImageView.center = CGPointMake(imageview.frame.size.width/2,imageview.frame.size.height/2);
        UIView *view = [[UIView alloc] initWithFrame:renderImageView.frame];
        view.backgroundColor = [UIColor clearColor];
        [view addSubview:renderImageView];

        [renderImageView release];
        [image release];
        imageview.image = nil;

        UIGraphicsBeginImageContext(imageview.frame.size);
        CGContextRef context1 = UIGraphicsGetCurrentContext();
        [view.layer renderInContext:context1];
        UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        [view release];

        [result drawAtPoint:imageview.frame.origin];
    }

}
UIImage *combinedImage2 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(combinedImage2, nil, nil, nil);



if (groupBgImage2) {
    [groupBgImage2 removeFromSuperview];
    [groupBgImage2 release];
    groupBgImage2 = nil;
}
groupBgImage2 = [[UIImageView alloc] initWithImage:combinedImage2];
[self.slideView.selectedView insertSubview:groupBgImage2 aboveSubview:selectedItem ];

}

于 2013-02-25T05:14:08.247 回答