-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

私は答えを割った。@ 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 に答える
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 に答える