2011-11-22 67 views
1

我正在爲使用SDK 4.0編寫iPhone應用程序,需要下載,重新調整大小並逐個顯示許多圖像。覆蓋ASIHTTPRequest的方法

我試圖用簡單的ASIHTTPRequest來做到這一點,但這些操作結果非常昂貴。所以我創建了繼承自ASIHTTPRequest的子類,我試圖覆蓋requestFinished。我雖然有這個問題。那麼..我需要以某種方式在我的UIImageView上設置該圖像。我真的不知道如何正確地做到這一點。

我試圖在我的子類中創建UIImage屬性,並將我的重新調整大小的圖像放在那裏,然後從請求中取出它。唉,它會導致問題。我得到EXC_BAD_ACCESS。我想這種方法可能會因併發性而變得很危險。有沒有簡單和安全的方法來做到這一點?我想過分析該圖像到NSData,並以某種方式切換請求響應。

編輯:JosephH:我做了你在那裏寫的東西。它幫助小圖片 - 我的意思是這不需要調整大小。所以當我添加調整大小 - 它又一次落後了。下面是一些代碼:

#import "BackgroundHTTPRequest.h" 

@implementation BackgroundHTTPRequest 
@synthesize myimg; 

-(UIImage *)imageWithImage:(UIImage*)imagee scaledToSize:(CGSize)newSize 
{ 
    // Create a bitmap context. 
    UIGraphicsBeginImageContextWithOptions(newSize, YES, [UIScreen mainScreen].scale); 
    [imagee drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; 
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return newImage; 

} 


- (void)requestFinished{ 
    NSData *responseData = [self responseData]; 
    UIImage *tempimg = [UIImage imageWithData:responseData]; 

    CGFloat ratio = tempimg.size.height/tempimg.size.width; 
    CGFloat widthmax = 320; 
    CGFloat heightmax = widthmax * ratio; 
    myimg = [self imageWithImage:tempimg scaledToSize:CGSizeMake(widthmax, heightmax)]; 
    [super requestFinished]; 
} 

- (void)dealloc{ 
    self.myimg = nil; 
    [super dealloc]; 
} 
@end 

而且一些代碼它發生在哪裏:

- (IBAction)grabURLInBackground:(NSURL *)url 
{ 
    [self.myrequest setDelegate:nil]; 
    [self.myrequest cancel]; 
    [self.myrequest release]; 


    self.myrequest = [[BackgroundHTTPRequest requestWithURL:url] retain];  

    [myrequest setDelegate:self]; 
    [myrequest startAsynchronous]; 

} 

- (void)requestFinished:(BackgroundHTTPRequest *)request 
{ 
    // Use when fetching binary data 
    if ((NSNull *)self == [NSNull null]){ 
     [request clearDelegatesAndCancel]; 
    } else { 
     if (self.image) 
      self.image.image = myrequest.myimg; 
    } 
    self.myrequest = nil; 
} 

另外添加一行

[self.myrequest setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy]; 

沒有幫助。

回答

0

你可以嘗試在看我這個答案公佈不是代碼:

How? UITableViewCell with UIImageView asynchronously loaded via ASINetworkQueue

而不是繼承ASIHTTPRequest,它的子類的UIImageView,這似乎很好地工作。

您可能可以使其以其他方式工作,但您的ASIHTTPRequest將不得不保留對UIImageView的引用...但您沒有向我們展示任何代碼或告訴我們它在哪裏/如何崩潰,所以我看到的任何東西都只會是瘋狂的猜測。