保存并加载UIImageView

时间:2015-09-03 05:32:12

标签: ios xcode uiimageview save load

我想在UIImageView中保存并加载图片 我已经尝试了一些代码,但它没有用。 我试过的代码:

保存:

UIImage *image = ...;
NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:@"image.png"];
[UIImagePNGRepresentation(image) writeToFile:cachedImagePath atomically:YES];

负载:

NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:@"image.png"];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfFile:cachedImagePath]];

但这些代码不起作用!

2 个答案:

答案 0 :(得分:1)

我希望您想将图像保存在文档目录文件夹中 这是保存图像的方法:

- (void)saveImage: (UIImage*)image
{
if (image != nil)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithString: @"test.png"] ];
NSData* data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
}
}

从文档目录中获取图像:

- (UIImage*)loadImage
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithString: @"test.png"] ];
UIImage* image = [UIImage imageWithContentsOfFile:path];
return image;
}

希望这会有所帮助

答案 1 :(得分:0)

全球声明imageView

UIImageView *imageView;//if u create imageView in interfaceBuilder. then u can replace this line by creating outlet of ur imageView

用于加载图片:

//Loading image in imageView
NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:@"image.png"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:cachedImagePath])
{
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfFile:cachedImagePath]];
    if (image)
    {
        imageView = [[UIImageView alloc]init];//Dont do this, if u created imageView using interFace builder
        [imageView setFrame:CGRectMake(50, 100, 200, 200)];//Dont do this, if u created imageView using interFace builder
        [self.view addSubview:imageView];//Dont do this, if u created imageView using interFace builder
        [imageView setImage:image];
    }
    else
    {
        NSLog(@"Unsupported File");
    }
}
else
{
    NSLog(@"Image not Found in the path");
}

用于保存图片:

UIImage *image = imageView.image;
if (image)
{
    NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:@"imageSaved.png"];
    [UIImagePNGRepresentation(image) writeToFile:cachedImagePath atomically:YES];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:cachedImagePath])
    {
        NSLog(@"File Created");
    }
}
else
{
    NSLog(@"No image to save");
}