当前位置: 动力学知识库 > 问答 > 编程问答 >

iphone - How to save and load Image in App?

问题描述:

i need to make app like penultimate. There should be a gallery and user can save pictures in it, not to camera roll. I already make drawing functions but can't save images into app. I have't any ideas how to do that. I already saw all questions like this in stack overflow. I don't want to use core data and tableview.

So, if you know how to do that, please help me)))

网友答案:

If you dont want use any databases. then have an option to save Images in ur Apps Document Directory and retrieve it from document Directory whenever needed.

i have written some methods to save image into doc director and retrieve it in my app.

These are as follows : in FileHandler.h I have declared these methods

#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

and in FileHandler.m i have these methods defined:

#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
    {
        [email protected]"";
    }

    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

Where I want to save image tomy document dir i use save method and for retrieving images i use getImageFrom..... method.

I hope this will helps u more

分享给朋友:
您可能感兴趣的文章:
随机阅读: