本文给大家分享了2则使用Objective C从远程url下载图片的方法,都是个人项目中使用的,汇总下推荐给大家,有需要的小伙伴可以参考下。
Objective C从远程url下载图片
- (UIImage *) getImageFromURL: (NSString *)theURL {
UIImage *theImage = NULL;
NSString *imageFileName = [BT_strings getFileNameFromURL:theURL];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:theURL]];
theImage = [[UIImage alloc] initWithData:imageData];
[BT_fileManager saveImageToFile:theImage fileName:imageFileName];
return theImage;
}
objective C从远程地址获取图片并修改尺寸
NSString* imageURL = [NSString stringWithFormat: @"http://theimageurl.com/?id=%@", [[resultsEntries objectAtIndex:0] objectForKey: @"image_large"]];
NSData* imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:imageURL]];
UIImage* image = [[UIImage alloc] initWithData:imageData];
// resize image
CGSize newSize = CGSizeMake(100, 100);
UIGraphicsBeginImageContext( newSize );// a CGSize that has the size you want
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
//image is the original UIImage
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imageHeight = image.size.height;
[imageMain setImage:newImage];
[imageData release];
[image release];
以上所述就是本文的全部内容了,希望大家能够喜欢。
沃梦达教程
本文标题为:Objective C从远程url下载图片方法汇总
基础教程推荐
猜你喜欢
- Android实现短信验证码输入框 2023-04-29
- iOS中如何判断当前网络环境是2G/3G/4G/5G/WiFi 2023-06-18
- iOS Crash常规跟踪方法及Bugly集成运用详细介绍 2023-01-18
- Android开发Compose集成高德地图实例 2023-06-15
- iOS开发 全机型适配解决方法 2023-01-14
- iOS开发使用XML解析网络数据 2022-11-12
- Flutter进阶之实现动画效果(三) 2022-10-28
- MVVMLight项目Model View结构及全局视图模型注入器 2023-05-07
- Android Compose自定义TextField实现自定义的输入框 2023-05-13
- IOS获取系统相册中照片的示例代码 2023-01-03