iphone - Posso carregar um UIImage de um URL?
uiimagepickercontroller (8)
A melhor e mais fácil maneira de carregar imagens via URL é através deste Código:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *data =[NSData dataWithContentsOfURL:[NSURL URLWithString:imgUrl]];
dispatch_async(dispatch_get_main_queue(), ^{
imgView.image= [UIImage imageWithData:data];
});
});
Substitua imgUrl
pelo seu ImageURL
Substitua imgView
pelo seu UIImageView
.
Ele irá carregar a imagem em outro segmento, por isso não vai diminuir a carga do seu aplicativo.
Eu tenho um URL para uma imagem (consegui-lo de UIImagePickerController), mas não tenho mais a imagem na memória (o URL foi salvo de uma execução anterior do aplicativo). Posso recarregar o UIImage a partir do URL novamente?
Eu vejo que UIImage tem um imageWithContentsOfFile: mas eu tenho um URL. Posso usar o dataWithContentsOfURL do NSData: para ler o URL?
EDIT1
com base na resposta do @ Daniel eu tentei o seguinte código, mas não funciona ...
NSLog(@"%s %@", __PRETTY_FUNCTION__, photoURL);
if (photoURL) {
NSURL* aURL = [NSURL URLWithString:photoURL];
NSData* data = [[NSData alloc] initWithContentsOfURL:aURL];
self.photoImage = [UIImage imageWithData:data];
[data release];
}
Quando eu corri, o console mostra:
-[PhotoBox willMoveToWindow:] file://localhost/Users/gary/Library/Application%20Support/iPhone%20Simulator/3.2/Media/DCIM/100APPLE/IMG_0004.JPG
*** -[NSURL length]: unrecognized selector sent to instance 0x536fbe0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL length]: unrecognized selector sent to instance 0x536fbe0'
Olhando para a pilha de chamadas, eu estou chamando URLWithString, que chama URLWithString: relativeToURL :, então initWithString: relativeToURL :, então _CFStringIsLegalURLString, então CFStringGetLength, em seguida, forwarding_prep_0 , em seguida, encaminhando , então - [NSObject doesNotRecognizeSelector].
Alguma idéia do porque meu NSString (o endereço do photoURL é 0x536fbe0) não responde ao tamanho? Por que diz que não responde a - [comprimento NSURL]? Não sabe que param é um NSString, não um NSURL?
EDIT2
OK, o único problema com o código é a string para conversão de URL. Se eu codificar a string, tudo mais funciona bem. Então, algo está errado com o meu NSString e se eu não consigo descobrir, eu acho que isso deve ser uma questão diferente. Com essa linha inserida (colei o caminho do log do console acima), funciona bem:
photoURL = @"file://localhost/Users/gary/Library/Application%20Support/iPhone%20Simulator/3.2/Media/DCIM/100APPLE/IMG_0004.JPG";
Certifique-se de ativar essas configurações no iOS 9:
Configurações de segurança de transporte do aplicativo no Info.plist para garantir o carregamento da imagem do URL para permitir a imagem de download e defini-la.
E escreva este código:
NSURL *url = [[NSURL alloc]initWithString:@"http://feelgrafix.com/data/images/images-1.jpg"];
NSData *data =[NSData dataWithContentsOfURL:url];
quickViewImage.image = [UIImage imageWithData:data];
E a versão rápida:
let url = NSURL.URLWithString("http://live-wallpaper.net/iphone/img/app/i/p/iphone-4s-wallpapers-mobile-backgrounds-dark_2466f886de3472ef1fa968033f1da3e1_raw_1087fae1932cec8837695934b7eb1250_raw.jpg");
var err: NSError?
var imageData :NSData = NSData.dataWithContentsOfURL(url,options: NSDataReadingOptions.DataReadingMappedIfSafe, error: &err)
var bgImage = UIImage(data:imageData)
Experimente este código, você pode definir o carregamento de imagens com ele, para que os usuários saibam que seu aplicativo está carregando uma imagem do URL:
UIImageView *yourImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"loading.png"]];
[yourImageView setContentMode:UIViewContentModeScaleAspectFit];
//Request image data from the URL:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://yourdomain.com/yourimg.png"]];
dispatch_async(dispatch_get_main_queue(), ^{
if (imgData)
{
//Load the data into an UIImage:
UIImage *image = [UIImage imageWithData:imgData];
//Check if your image loaded successfully:
if (image)
{
yourImageView.image = image;
}
else
{
//Failed to load the data into an UIImage:
yourImageView.image = [UIImage imageNamed:@"no-data-image.png"];
}
}
else
{
//Failed to get the image data:
yourImageView.image = [UIImage imageNamed:@"no-data-image.png"];
}
});
});
Se você está absolutamente seguro de que a NSURL é uma URL de arquivo, ou seja, [url isFileURL]
tem a garantia de retornar true no seu caso, então você pode simplesmente usar:
[UIImage imageWithContentsOfFile:url.path]
Você pode experimentar o SDWebImage , ele fornece:
- Carregamento assíncrono
- Cache para uso offline
- Coloque a imagem do suporte para aparecer durante o carregamento
- Funciona bem com o UITableView
Exemplo rápido:
[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
obtenha DLImageLoader e tente seguir o código
[DLImageLoader loadImageFromURL:imageURL
completed:^(NSError *error, NSData *imgData) {
imageView.image = [UIImage imageWithData:imgData];
[imageView setContentMode:UIViewContentModeCenter];
}];
Outro exemplo típico do mundo real de usar o DLImageLoader, que pode ajudar alguém ...
PFObject *aFacebookUser = [self.fbFriends objectAtIndex:thisRow];
NSString *facebookImageURL = [NSString stringWithFormat:
@"http://graph.facebook.com/%@/picture?type=large",
[aFacebookUser objectForKey:@"id"] ];
__weak UIImageView *loadMe = self.userSmallAvatarImage;
// ~~note~~ you my, but usually DO NOT, want a weak ref
[DLImageLoader loadImageFromURL:facebookImageURL
completed:^(NSError *error, NSData *imgData)
{
if ( loadMe == nil ) return;
if (error == nil)
{
UIImage *image = [UIImage imageWithData:imgData];
image = [image ourImageScaler];
loadMe.image = image;
}
else
{
// an error when loading the image from the net
}
}];
Como eu mencionei acima outra grande biblioteca para considerar estes dias é Haneke (infelizmente não é tão leve).
AFNetworking fornece carregamento de imagem assíncrona para um UIImageView com suporte de espaço reservado. Ele também suporta redes assíncronas para trabalhar com APIs em geral.