假設我創建了一個新線程並將一個對象傳遞給它。如何釋放它?在多線程中釋放對象
- (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;
}
我的理解是這個函數也在後臺線程中調用。這裏pdf是類變量。 我做錯了什麼?
ohhhh呀它清楚地提到那裏... – saurabh