相关疑难解决方法(0)

iOS:从网址下载图片并保存在设备中

我想从网址http://a3.twimg.com/profile_images/414797877/05052008321_bigger.jpg下载图片

我使用以下代码,但图像未保存在设备中.我想知道我做错了什么.

 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://a3.twimg.com/profile_images/414797877/05052008321_bigger.jpg"]];
 [NSURLConnection connectionWithRequest:request delegate:self];

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];
 NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:@"pkm.jpg"];
 NSData *thedata = NULL;
 [thedata writeToFile:localFilePath atomically:YES];

 UIImage *img = [[UIImage alloc] initWithData:thedata];
Run Code Online (Sandbox Code Playgroud)

iphone cocoa-touch image ipad ios

46
推荐指数
3
解决办法
9万
查看次数

标签 统计

cocoa-touch ×1

image ×1

ios ×1

ipad ×1

iphone ×1