我正在开发一个iOS聊天应用程序。我想用斜杠键盘。你能指导我如何在我的iOS编程中使用gif映像吗?实际上,在从斜杠键盘文件中选择一个gif之后,我得到的链接并没有在代码中为我提供图像。请引导我。
对于http://tapslash.com/m/giphy/NycV9yzKiiBR6,这是一个gif链接,但是我无法在Xcode中获得图像。
NSURL *url=[NSURL URLWithString:@"http://tapslash.com/m/giphy/10k8HMhtzzk73W"];
NSData * imageData = [NSData dataWithContentsOfURL:url];
UIImage * image = [UIImage imageWithData:imageData];
总是得到图像零
发布于 2015-12-26 11:27:52
该链接不是指向图像的链接,而是放到包含该图像的网页上。一定要从链接中获取NSData,它需要以.gif或类似的方式结束。在您的示例代码中,我会将其更改为
NSURL *url=[NSURL URLWithString:@"http://media0.giphy.com/media/10k8HMhtzzk73W/giphy.gif"];
NSData * imageData = [NSData dataWithContentsOfURL:url];
UIImage * image = [UIImage imageWithData:imageData];
如果您有URL,那么要得到一个http://tapslash.com/m/giphy/10k8HMhtzzk73W
url,就必须在最后得到这个部分。
NSArray *splitURL = [stringContainingURL componentsSeparatedBy:@"/"];
NSString *identifier = [splitURL lastObject]; // Written in browser, not sure if exists. If not use [splitURL objectAtIndex:splitURL.count - 1];
NSString *goodURLString = [NSString stringWithFormat:@"http://media0.giphy.com/media/%@/giphy.gif", identifier];
// Should return: http://media0.giphy.com/media/10k8HMhtzzk73W/giphy.gif
https://stackoverflow.com/questions/34472106
复制相似问题