如何在App中保存和加载图像?

时间:2013-01-12 18:17:39

标签: iphone ios xcode ipad ios6

我需要像倒数第二个那样制作app。应该有一个画廊,用户可以在其中保存图片,而不是相机胶卷。我已经制作了绘图功能,但无法将图像保存到应用程序中。我没有任何想法如何做到这一点。我已经在堆栈溢出中看到了所有这样的问题。我不想使用核心数据和tableview。 所以,如果你知道如何做到这一点,请帮助我)))

1 个答案:

答案 0 :(得分:0)

如果您不想使用任何数据库。然后可以选择在您的Apps文档目录中保存图像,并在需要时从文档目录中检索它。

我已经写了一些方法将图像保存到doc director中并在我的应用程序中检索它。

这些如下: 在FileHandler.h中我已经声明了这些方法

#import <Foundation/Foundation.h>


@interface FileHandler : NSObject
{

}

/* Property */
@property (nonatomic, retain) NSString *dirPath;
//@property (nonatomic, retain) NSString *fileName;

/* init method */

-(id)init;


/* Other Contents */
-(void)createImageFilterDirectory;

-(void)createDirectoryAtPath:(NSString*)folderPath;

-(NSString*)getFilteredImageFilePathWithFileName:(NSString*)fileName;

-(void)saveImageToDocDir:(UIImage*)aImage  withFileName:(NSString*)file;

-(UIImage*)getImageWithFileName:(NSString*)imageFileName;

-(BOOL)removeImageWithFileName:(NSString*)fName;

-(void)deleteAllDirContents;

-(NSArray*)getAllDirContents;

-(NSArray*)getAllImageFromDir;

-(BOOL)deleteDir;

@end

并在FileHandler.m中定义了这些方法:

#import "FileHandler.h"

#define ImageFilterDir   @"ImageFilterDir"

@implementation FileHandler
@synthesize dirPath;//, fileName;

/* int implementation */
-(id)init
{
    self = [super init];

    if(self) {
        //Custom Initialization
        self.dirPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
        [self createDirectoryAtPath:self.dirPath];

    }

    return self;
}


/* Create Dir of Name ImageFilterDir */
-(void)createImageFilterDirectory
{
    self.dirPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    [self createDirectoryAtPath:self.dirPath];
}

/* Creating Director if not exists */
-(void)createDirectoryAtPath:(NSString *)folderPath
{
    NSString *dPath = [folderPath stringByAppendingPathComponent:ImageFilterDir];
    NSError *error=nil;

    if(![[NSFileManager defaultManager] fileExistsAtPath:dPath])
    {
        [[NSFileManager defaultManager]  createDirectoryAtPath:dPath withIntermediateDirectories:NO attributes:nil error:&error];
        NSLog(@"Directory %@ Created..",dPath);

    }
    else
    {
        NSLog(@"Directory Exists Already..");
    }

}


/* Get File Path with File Name */
-(NSString*)getFilteredImageFilePathWithFileName:(NSString *)fileName
{
    NSString *documentDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filterImageDir = [documentDir stringByAppendingPathComponent:ImageFilterDir];
    NSString *filePath=nil;

    if([[NSFileManager defaultManager] fileExistsAtPath:filterImageDir])
    {
        filePath = [filterImageDir stringByAppendingPathComponent:fileName];
    }
    else
    {
        filePath=@"";
    }

    return filePath;
}


/* Save Image to Dir */
-(void)saveImageToDocDir:(UIImage *)aImage withFileName:(NSString *)file
{
    NSString *filePath = [self getFilteredImageFilePathWithFileName:file];
    NSData *imageData = UIImagePNGRepresentation(aImage);
    [imageData writeToFile:filePath atomically:YES];

    NSLog(@"Writing File => %@ .... ",file);
}


/* Get Image With File Name */
-(UIImage*)getImageWithFileName:(NSString *)imageFileName
{
    NSString *path = [self getFilteredImageFilePathWithFileName:imageFileName];
    NSData *fData = [NSData dataWithContentsOfFile:path];

    UIImage *image = [UIImage imageWithData:fData];

    return image;
}


/* Remove Image With File Name */
-(BOOL)removeImageWithFileName:(NSString *)fName
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *path = [self getFilteredImageFilePathWithFileName:fName];
    NSError *error=nil;
    if([fileManager fileExistsAtPath:path])
        return [fileManager removeItemAtPath:path error:&error];
    else
        return NO;
}

/* Delete All Dir Contents */
-(void)deleteAllDirContents
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *imgFilterDir = [self.dirPath stringByAppendingPathComponent:ImageFilterDir];
    NSError *error=nil;
    NSArray *arr = [fileManager contentsOfDirectoryAtPath:imgFilterDir error:&error];

    for (NSString *file in arr) {
        NSString *p = [self getFilteredImageFilePathWithFileName:file];
        if([fileManager fileExistsAtPath:p])
        {
            [fileManager removeItemAtPath:p error:&error];
        }

    }

}

/* Get All Dir Contents. Returns an Array of Filenames */
-(NSArray*)getAllDirContents
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *imgFilterDir = [self.dirPath stringByAppendingPathComponent:ImageFilterDir];
    NSError *error=nil;
    NSArray *arr = [fileManager contentsOfDirectoryAtPath:imgFilterDir error:&error];

    return arr;
}


/* Get All Dir Contents. Returns an Array of Image */
-(NSArray*)getAllImageFromDir
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *imgFilterDir = [self.dirPath stringByAppendingPathComponent:ImageFilterDir];
    NSError *error=nil;
    NSArray *arr = [fileManager contentsOfDirectoryAtPath:imgFilterDir error:&error];

    NSMutableArray *imgArr = [[NSMutableArray alloc]init];

    for (NSString *file in arr)
    {
        NSString *p = [self getFilteredImageFilePathWithFileName:file];
        if([fileManager fileExistsAtPath:p])
        {
            NSData *imgData = [NSData dataWithContentsOfFile:p];
            UIImage *image =[UIImage imageWithData:imgData];
            [imgArr addObject:image];
        }

    }

    return [imgArr autorelease];
}


/* Delete Dir */
-(BOOL)deleteDir
{
    [self deleteAllDirContents];

    NSString *f = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    f=[f stringByAppendingPathComponent:ImageFilterDir];
    NSError *error=nil;
    if([[NSFileManager defaultManager] fileExistsAtPath:f])
    {
       return [[NSFileManager defaultManager] removeItemAtPath:f error:&error];
    }
    else
    {
        return NO;
    }

}

-(void)dealloc
{
   // [fileName release];
    [dirPath release];

    [super dealloc];
}

@end

我要保存图像tomy文档目录我使用保存方法和检索图像我使用getImageFrom .....方法。

我希望这会对你有所帮助