Вам придется использовать NSURLConnection. Это довольно просто, но более сложно, чем метод NSData.
Сначала создайте NSURLConnection:
NSMutableData *receivedData;
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:imgLink]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
receivedData=[[NSMutableData data] retain];
} else {
// inform the user that the download could not be made
}
Теперь добавьте в заголовок вашего класса и реализуйте следующие методы:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
Первый должен добавить данные, как показано ниже, а последний должен создать и отобразить изображение.
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
{
// append the new data to the receivedData
// receivedData is declared as a method instance elsewhere
[receivedData appendData:data];
}
Подробнее см. в этом документе .