2011-11-22 21 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的引用...但您没有向我们展示任何代码或告诉我们它在哪里/如何崩溃,所以我看到的任何东西都只会是疯狂的猜测。