English 中文(简体)
多读物释放物体
原标题:release objects in multithreading

附录一制造了新的read子,并穿过物体。 如何释放?

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    float x=    scrollView.contentOffset.x/(self.view.frame.size.width/2 + 40 );
    int pg=round(x);
    pg=abs(x);
    currentPage=pg;
    if(pg!=currentIndex && pg+1 < pageCounter)
    {
        currentIndex=   pg; 
        NSNumber *num=[NSNumber numberWithInt:pg+1];
        [NSThread detachNewThreadSelector:@selector(getImageForPage:) toTarget:self withObject:num];
    }
}

工作背景

-(void)getImageForPage:(NSNumber*)page{
    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
    int pg=[page intValue];
    UIButton *btn = (UIButton*)[scrl viewWithTag:pg];
    UIImageView *imgV = (UIImageView *)[btn viewWithTag:1111];      
    if(imgV.image==nil)
    {
        NSLog(@"currnt visible page %d ",currentPage);
        if(!isWorking)
        {
        UIImage *im=    [self imageFromPDFforPage:pg+1];        
            imgV.image = im;
        }
        else
            [self performSelector:@selector(getImageForPage:) withObject:[NSNumber numberWithInt:pg+1] afterDelay:3];
    }
    [pool drain];
}

我应称[集合排水];或 [集合释放];

- (UIImage *)imageFromPDFforPage:(int)page {
    isWorking=YES;
    CGPDFPageRef pageRef = CGPDFDocumentGetPage(pdf, page);
    CGRect pageRect = CGPDFPageGetBoxRect(pageRef, kCGPDFCropBox);  
    NSLog(@"page %d, w=  %f h= %f",page, pageRect.size.width,pageRect.size.height);
    UIGraphicsBeginImageContext(pageRect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, CGRectGetMinX(pageRect),CGRectGetMaxY(pageRect));
    CGContextScaleCTM(context, 1, -1);  
    CGContextTranslateCTM(context, -(pageRect.origin.x), -(pageRect.origin.y));     
    CGAffineTransform transform = aspectFit(CGPDFPageGetBoxRect(pageRef, kCGPDFCropBox),
                                            CGContextGetClipBoundingBox(context));
    CGContextConcatCTM(context, transform);
    CGContextDrawPDFPage(context, pageRef);     
    UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    isWorking=NO;
    return finalImage;          
} 

My understanding is this function is also called in background threads. Here pdf is class variable. am I doing anything wrong?

最佳回答
问题回答

(1) In a non-garbage-collected environment [pool drain] and [pool release] behave the same.

(2) The object you are passing in the separate thread is an autoreleased object, so you do not need to release it in scrollViewDidScroll:(UIScrollView *)scrollView.

除此以外,如果你把支持全球发展运动的SOS版本作为目标,那么你就希望使用这一技术而不是read。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签