iphone将视图绘制成UIImage(内存警告)

时间:2013-01-18 15:22:57

标签: memory uiimageview

我想用UIImageView绘制图像和混合,它工作正常。     但是,当我在循环中运行此函数时,它显示内存释放。     我不知道为什么会这样,以及如何解决它。     我尝试了两种方法     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;

}

1 个答案:

答案 0 :(得分:0)

使用仪器(泄漏)运行它,它可以让您更好地了解泄漏内存的对象是什么。查看代码很难确定问题所在。