Drawrect causing memory issues
<p>Currently I am using UIView instead of UIImageview due to Memory consumption in large scale images. following is same code I am using.</p>

<pre><code>- (void)drawRect:(CGRect)rect
{
   CGContextRef context = UIGraphicsGetCurrentContext();
   CGContextClearRect(context, rect);
   [myImage drawInRect:rect];
}

-(void) SetImage:(UIImage*) aImage
{
    if(!aImage)
        return;

    if(myImage)
    {
        [myImage release];
        myImage = nil;
    }

    myImage = [[[UIImage alloc]initWithCGImage:aImage.CGImage] retain];
    [self setNeedsDisplay];
} 
</code></pre>

<p>This is causing now memory leak of 8 MB ( checked with Instrument ) every time when Update and set the same image again. if I comment </p>

<pre><code>[self setNeedsDisplay];
</code></pre>

<p>There is no leak. can anyone help me here if I am doing something wrong. OR can anyone help me to Subclass UIImageview to handle large scale image.</p>

<pre><code>// Calling functions
    -(void) FitToCardStart
    {
        UIImage* temp = ScaleImage([iImageBgView GetImage]);
        [iImageBgView SetImage:temp];
        [temp release];
        temp = nil;

    }

// ScaleImage

UIImage* ScaleImage(UIImage* image) 
{
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    int kMaxResolution = 1800; 

    CGImageRef imgRef = image.CGImage;
    CGFloat width = CGImageGetWidth(imgRef);
    CGFloat height = CGImageGetHeight(imgRef);

    CGAffineTransform transform = CGAffineTransformIdentity;

    CGRect bounds = CGRectMake(0, 0, width, height);

    if (width &lt; kMaxResolution || height &lt; kMaxResolution) 
    {
        CGFloat ratio = width/height;
        if (ratio &gt; 1) 
        {
            bounds.size.width = kMaxResolution;
            bounds.size.height = bounds.size.width / ratio;
        }
        else 
        {
            bounds.size.height = kMaxResolution;    
            bounds.size.width = bounds.size.height * ratio;
        }
    }

    CGFloat scaleRatio = bounds.size.width / width;
    CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));

    UIImageOrientation orient = image.imageOrientation;

    switch(orient) 
    {

        case UIImageOrientationUp: //default
            transform = CGAffineTransformIdentity;
            break;

        default:
            [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];
    }

    UIGraphicsBeginImageContext(bounds.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextScaleCTM(context, scaleRatio, -scaleRatio);
    CGContextTranslateCTM(context, 0, -height);

    CGContextConcatCTM(context, transform);
    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
    UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
    UIImage* temp = [[[UIImage alloc] initWithCGImage:imageCopy.CGImage] retain];
    CGImageRelease(imgRef);
    CGContextRelease(context);  

    [pool release];

    return temp;
} 
</code></pre>

<p>Thanks,</p>

<p>Sagar</p>
