0

UIImageView で画像を描画してブレンドしたいのですが、うまくいきます。ただし、この関数をループで実行すると、メモリの未解放が表示されます。なぜこれが起こるのか、それを解決する方法はわかりません。私は2つの方法を試しました 1.UIGraphicsGetImageFromCurrentImageContext
2.CGBitmapContextCreate 両方とも、実行ごとにメモリ使用量が増加します。約 30 回実行すると、メモリ警告が表示される場合があります。私はそれに多くの時間を費やしましたが、解決できません。助けてください、ありがとう。

    -(int) affineImage2:(UIImage *)image initAffineTransform:(CGAffineTransform)initTransform AffineTransform:(CGAffineTransform)transform Caption:(NSString*)caption Center:(CGPoint)center Path:(NSString*)path FrameImage:(UIImage*) frameImage
{

int jpgbyte;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

if ([NSThread isMainThread]) {
    NSLog(@"YES if ([NSThread isMainThread])");
}else{
     NSLog(@"NOT if ([NSThread isMainThread])");
}
@autoreleasepool
{


    UIImageView* view1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,frameImage.size.width,frameImage.size.height)];
    view1.contentMode=UIViewContentModeTop;
UIImageView* imageView = [[UIImageView alloc] initWithImage:image];
    NSLog(@"[image release];");

UIImageView* frameView   = [[UIImageView alloc] initWithImage:frameImage];
    NSLog(@"[image release];");
frameView.alpha = 1;//0.5;  // Customize the opacity of the top image.

float angle=atan2(transform.b, transform.a);
float scale = sqrt(transform.a*transform.a + transform.c*transform.c);
float initScale = sqrt(initTransform.a*initTransform.a + initTransform.c*initTransform.c);
CGAffineTransform t = CGAffineTransformIdentity;


CGRect fullScreenRect = [[UIScreen mainScreen] bounds];
    float centery;
    if((int)fullScreenRect.size.height==568)
    {  NSLog(@"1");
        centery=PREVIEW_RECT_CEN_Y+44.0;
    }else{
        centery=PREVIEW_RECT_CEN_Y;
    }



float imageScale;
    NSLog(@"image=w %f h=%f",image.size.width,image.size.height);
if(image.size.width>image.size.height)
{   imageScale=FULL_RECT_WD/image.size.height;
}else{
     imageScale=FULL_RECT_WD/image.size.width;
}
t = CGAffineTransformConcat(t, CGAffineTransformMakeScale(imageScale*scale/initScale,imageScale*scale/initScale));//
t = CGAffineTransformConcat(t, CGAffineTransformMakeRotation(angle));

imageView.transform=t;
    imageView.center=CGPointMake( (center.x-PREVIEW_RECT_CEN_X)*640/320.0*FULL_RECT_WD/PREVIEW_RECT_WD+ FULL_RECT_CEN_X ,(center.y-centery)*960/480.0*FULL_RECT_WD/PREVIEW_RECT_WD+FULL_RECT_CEN_Y);
[view1 addSubview:imageView];
[imageView release];

 UILabel *editCaption=[[UILabel alloc] initWithFrame:CGRectMake(FULL_RECT_CEN_X-FULL_RECT_WD/2, FULL_RECT_CEN_Y+FULL_RECT_HT/2*0.9, FULL_RECT_WD, FULL_RECT_HT/2.4)];
NSString *title = [NSString stringWithFormat:@"UILabel Sample 1111"];

[editCaption setText:caption];
[editCaption setNumberOfLines:3];
[editCaption setFont:[UIFont fontWithName:@"STHeitiTC-Medium" size:CAPTION_FONT_SIZE*(FULL_RECT_WD/PREVIEW_RECT_WD)*2  ]];
[editCaption setTextColor:[UIColor blackColor]];
[editCaption setBackgroundColor:[UIColor clearColor]];
[frameView addSubview:editCaption];
[editCaption release];

[view1 addSubview:frameView];




#if 0

   // CGSize size = CGSizeMake(width, height);

    if (NULL != UIGraphicsBeginImageContextWithOptions)
        UIGraphicsBeginImageContextWithOptions(view1.frame.size, NO, 1);
    else
        UIGraphicsBeginImageContext(view1.frame.size);



[view1.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* blendedImage =
    UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();


 #else
    size_t width = view1.bounds.size.width;
    size_t height = view1.bounds.size.height;

    unsigned char *imageBuffer = (unsigned char *)malloc(width*height*4);
    CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef imageContext =
    CGBitmapContextCreate(imageBuffer, width, height, 8, width*4, colourSpace,
                          kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Little);

    CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0,height);
    CGContextConcatCTM(imageContext, flipVertical);


    CGColorSpaceRelease(colourSpace);

    [view1.layer renderInContext:imageContext];

    CGImageRef outputImage = CGBitmapContextCreateImage(imageContext);
     UIImage* blendedImage = [UIImage imageWithCGImage:outputImage];
    CGImageRelease(outputImage);
    CGContextRelease(imageContext);
    frameView.layer.contents=nil;
    imageView.layer.contents=nil;
    view1.layer.contents=nil;


    free(imageBuffer);

#endif

    for (UIView *view2 in view1.subviews)
    {  [view2 removeFromSuperview];
        view2=nil;
    }



[frameView release];       frameView=nil;

     imageView=nil;
    view1.layer.contents=nil;        
    frameView.layer.contents=nil;
    imageView.layer.contents=nil;

[view1 release]; view1=nil;

// save to disk

 NSData* nsData1 = UIImageJPEGRepresentation(blendedImage,jpeg_quality);

  //  [blendedImage release];
   jpgbyte=nsData1.length;
    NSLog(@"jpgbyte=%d",jpgbyte);

if(![nsData1 writeToFile:path atomically:YES])
{
}




}
//[pool drain];

[pool release];
pool = nil;


return jpgbyte;

}

4

1 に答える 1

0

インストルメント (リーク) を使用して実行すると、どのオブジェクトがメモリ リークを起こしているかをよりよく知ることができます。コードを見て、問題がどこにあるかを特定するのは困難です。

于 2013-01-18T16:10:44.683 に答える