Objective C, Can't load an image from data returned by ASIHTTP request
- (vo开发者_如何学Goid)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSData *data = [NSData dataWithData:[request responseData]];
NSData *responseData = [request responseData];
UIImage *img = [[UIImage alloc ]imageWithData:data];//it blows up here
self.q.image = img;
[img release];
self.request = nil;
[delegate imageDidLoad:self.indexPathInTableView];
}
I am downloading an image data using ASIHTTP request and it worked fine. But If I try to create an image using the data from ASIHTTP request then it blows up..What is the problem? Thanks in advance...
Its because you are using imageWithData:
to create your image, you should use initWithData:
or just [UIImage imageWithData:data]
instead of alloc'ing first.
Your code doesn't work because imageWithData:
is a static method on UIImage that generates an autoreleased UIImage instance. Because it is a static method, not an instance method, it isn't a recognised selector on the UIImage instance you get when you call [UIImage alloc]
as the error says the it's an unrecognized selector.
you should call - (id)initWithData:(NSData *)data
.
[[UIImage alloc] initWithData:data];
or
[UIImage imageWithData:data];
精彩评论