//将图片保存到userDefaults
+ (void)SaveImageToLocal:(UIImage*)image Keys:(NSString*)key {
NSUserDefaults* preferences = [NSUserDefaults standardUserDefaults];
//[preferences persistentDomainForName:LocalPath];
[preferences setObject:UIImagePNGRepresentation(image) forKey:key];
}
//将图片保存到沙箱目录
-(void)saveImage:(UIImage *)image toFilePath:(NSString *)filePath
{
NSString *docDic=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)[0];
//这里自己起个名用于保存到目录的图片
NSString *picPath=[docDic stringByAppendingPathComponent:@"你的图片名"];
[UIImagePNGRepresentation(image) writeToFile:picPath atomically:YES];
}
//本地是否有相关图片
+ (BOOL)LocalHaveImage:(NSString*)key {
NSUserDefaults* preferences = [NSUserDefaults standardUserDefaults];
//[preferences persistentDomainForName:LocalPath];
NSData* imageData = [preferences objectForKey:key];
if (imageData) {
return YES;
}
return NO;
}//从本地获取图片
+ (UIImage*)GetImageFromLocal:(NSString*)key {
NSUserDefaults* preferences = [NSUserDefaults standardUserDefaults];
//[preferences persistentDomainForName:LocalPath];
NSData* imageData = [preferences objectForKey:key];
UIImage* image;
if (imageData) {
image = [UIImage imageWithData:imageData];
}
else {
NSLog(@"未从本地获得图片");
}
return image;
}
//通过url下载图片
-(UIImage *)downloadImageFromURL:(NSURL *)url
{
UIImage *img=[[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:url]];
return img;
}
本文详细介绍了如何使用Objective-C和Swift语言,在iOS和Android设备上将图片保存到本地和沙箱目录,并提供了一个简单的方法来检查和获取已保存的图片。此外,还展示了如何通过URL下载图片,适用于跨平台应用开发。

6万+

被折叠的 条评论
为什么被折叠?



