iphone 保存图片以及读取图片 demo
显示图片:思路:想利用fileManager将某一目录的文件夹中的图片名称保存在可变数组中,然后再利用循环,将图片保存到图片数组中,然后在显示出来。
 显示图片
显示图片-(IBAction) showImage:(id)sender
{
    
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *files = [NSArray arrayWithArray:[fileManager URLsForDirectory:NSMusicDirectory inDomains:NSUserDomainMask ]];
NSLog(@"files :%d",[files count]);
NSLog(@"the files is %@",[files objectAtIndex:0]);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
BOOL isDir = NO;
NSLog(@"%d",[paths count]);
    
NSError *error = nil;
NSArray *fileList = [[NSArray alloc] init];
NSMutableArray *filePath = [NSMutableArray arrayWithCapacity:100 ];
NSMutableArray *imgArray = [NSMutableArray arrayWithCapacity:200];
//NSArray *filePath = [[NSArray alloc] init];
fileList = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:&error];
for (NSString *file in fileList) {
NSLog(@"fileName is :%@",file);
[filePath addObject:[documentsDirectory stringByAppendingPathComponent:file]];
        
}
     
for (int i = 0; i<[fileList count]; i++)
{
NSString *filePathName = [filePath objectAtIndex:i];
if ([filePathName hasSuffix:@"png"]) {
UIImage *temp = [UIImage imageWithContentsOfFile:filePathName];
[imgArray addObject:temp];
}
}
    
[imgView1 setImage:[imgArray objectAtIndex:0]];
[imgView2 setImage:[imgArray objectAtIndex:1]];
[imgView3 setImage:[imgArray objectAtIndex:2]];
[imgView4 setImage:[imgArray objectAtIndex:3]];
[imgView5 setImage:[imgArray objectAtIndex:4]];
[imgView6 setImage:[imgArray objectAtIndex:5]];
[imgView7 setImage:[imgArray objectAtIndex:6]];
[imgView8 setImage:[imgArray objectAtIndex:7]];
[imgView9 setImage:[imgArray objectAtIndex:8]];
NSURL *url = [NSURL URLWithString:@"http://list.image.baidu.com/t/image_category/galleryimg/menstar/hk/luo_zhi_xiang.jpg"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [UIImage imageWithData:data];
    
NSLog(@"width:%f,heigh:%f",img.size.width,img.size.height);
/*测试NSHomeDirectory
NSString *homeDirectionary = [NSHomeDirectory() stringByAppendingPathComponent:@"a"];
NSLog(@"路径:%@",homeDirectionary);*/
    
/* 创建文件夹 */
NSString *newPath = [documentsDirectory stringByAppendingPathComponent:@"test"];
BOOL isOK = [fileManager createDirectoryAtPath:newPath attributes:nil];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"nodeOff.png"];
NSLog(@"imagePath:%@",imagePath);
NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
NSString *newPathCopy = [NSString stringWithFormat:@"%@/nodeOff.png",newPath];
NSLog(@"newPathCopy;%@",newPathCopy);
    
BOOL isSave = [fileManager createFileAtPath:newPathCopy contents:imageData attributes:nil];
NSLog(@"isSave:%@",isSave ? @"YES":@"NO");
[imgUrl setImage:img];
    
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *files = [NSArray arrayWithArray:[fileManager URLsForDirectory:NSMusicDirectory inDomains:NSUserDomainMask ]];
NSLog(@"files :%d",[files count]);
NSLog(@"the files is %@",[files objectAtIndex:0]);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
BOOL isDir = NO;
NSLog(@"%d",[paths count]);
NSError *error = nil;
NSArray *fileList = [[NSArray alloc] init];
NSMutableArray *filePath = [NSMutableArray arrayWithCapacity:100 ];
NSMutableArray *imgArray = [NSMutableArray arrayWithCapacity:200];
//NSArray *filePath = [[NSArray alloc] init];
fileList = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:&error];
for (NSString *file in fileList) {
NSLog(@"fileName is :%@",file);
[filePath addObject:[documentsDirectory stringByAppendingPathComponent:file]];
}
for (int i = 0; i<[fileList count]; i++)
{
NSString *filePathName = [filePath objectAtIndex:i];
if ([filePathName hasSuffix:@"png"]) {
UIImage *temp = [UIImage imageWithContentsOfFile:filePathName];
[imgArray addObject:temp];
}
}
[imgView1 setImage:[imgArray objectAtIndex:0]];
[imgView2 setImage:[imgArray objectAtIndex:1]];
[imgView3 setImage:[imgArray objectAtIndex:2]];
[imgView4 setImage:[imgArray objectAtIndex:3]];
[imgView5 setImage:[imgArray objectAtIndex:4]];
[imgView6 setImage:[imgArray objectAtIndex:5]];
[imgView7 setImage:[imgArray objectAtIndex:6]];
[imgView8 setImage:[imgArray objectAtIndex:7]];
[imgView9 setImage:[imgArray objectAtIndex:8]];
NSURL *url = [NSURL URLWithString:@"http://list.image.baidu.com/t/image_category/galleryimg/menstar/hk/luo_zhi_xiang.jpg"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [UIImage imageWithData:data];
NSLog(@"width:%f,heigh:%f",img.size.width,img.size.height);
/*测试NSHomeDirectory
NSString *homeDirectionary = [NSHomeDirectory() stringByAppendingPathComponent:@"a"];
NSLog(@"路径:%@",homeDirectionary);*/
/* 创建文件夹 */
NSString *newPath = [documentsDirectory stringByAppendingPathComponent:@"test"];
BOOL isOK = [fileManager createDirectoryAtPath:newPath attributes:nil];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"nodeOff.png"];
NSLog(@"imagePath:%@",imagePath);
NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
NSString *newPathCopy = [NSString stringWithFormat:@"%@/nodeOff.png",newPath];
NSLog(@"newPathCopy;%@",newPathCopy);
BOOL isSave = [fileManager createFileAtPath:newPathCopy contents:imageData attributes:nil];
NSLog(@"isSave:%@",isSave ? @"YES":@"NO");
[imgUrl setImage:img];
保存图片:思路,先将图片保存为NSdata,在保存到文件夹中
 保存图片
保存图片- (void)viewDidLoad {
[super viewDidLoad];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath1 = [documentsDirectory stringByAppendingPathComponent:@"left.png"];
NSString *savedImagePath2 = [documentsDirectory stringByAppendingPathComponent:@"right.png"];
NSString *savedImagePath3 = [documentsDirectory stringByAppendingPathComponent:@"right3.png"];
NSString *savedImagePath4 = [documentsDirectory stringByAppendingPathComponent:@"up3.png"];
NSString *savedImagePath5 = [documentsDirectory stringByAppendingPathComponent:@"nodeOff.png"];
NSString *savedImagePath6 = [documentsDirectory stringByAppendingPathComponent:@"nodeOn.png"];
NSString *savedImagePath7 = [documentsDirectory stringByAppendingPathComponent:@"webcam002.png"];
NSString *savedImagePath8 = [documentsDirectory stringByAppendingPathComponent:@"webcam003.png"];
NSString *savedImagePath9 = [documentsDirectory stringByAppendingPathComponent:@"plus3.png"];
    
//also be .jpg or another
UIImage *newImage1 = [UIImage imageNamed:@"left.png"];
UIImage *newImage2 = [UIImage imageNamed:@"right.png"];
UIImage *newImage3 = [UIImage imageNamed:@"right3.png"];
UIImage *newImage4 = [UIImage imageNamed:@"up3.png"];
UIImage *newImage5 = [UIImage imageNamed:@"nodeOff.png"];
UIImage *newImage6 = [UIImage imageNamed:@"nodeOn.png"];
UIImage *newImage7 = [UIImage imageNamed:@"webcam002.png"];
UIImage *newImage8 = [UIImage imageNamed:@"webcam003.png"];
UIImage *newImage9 = [UIImage imageNamed:@"plus3.png"];
    
NSData *imageData1 = UIImagePNGRepresentation(newImage1);
NSData *imageData2 = UIImagePNGRepresentation(newImage2);
NSData *imageData3 = UIImagePNGRepresentation(newImage3);
NSData *imageData4 = UIImagePNGRepresentation(newImage4);
NSData *imageData5 = UIImagePNGRepresentation(newImage5);
NSData *imageData6 = UIImagePNGRepresentation(newImage6);
NSData *imageData7 = UIImagePNGRepresentation(newImage7);
NSData *imageData8 = UIImagePNGRepresentation(newImage8);
NSData *imageData9 = UIImagePNGRepresentation(newImage9);
    
//UIImageJPEGRepresentation(image)
[imageData1 writeToFile:savedImagePath1 atomically:YES];
[imageData2 writeToFile:savedImagePath2 atomically:YES];
[imageData3 writeToFile:savedImagePath3 atomically:YES];
[imageData4 writeToFile:savedImagePath4 atomically:YES];
[imageData5 writeToFile:savedImagePath5 atomically:YES];
[imageData6 writeToFile:savedImagePath6 atomically:YES];
[imageData7 writeToFile:savedImagePath7 atomically:YES];
[imageData8 writeToFile:savedImagePath8 atomically:YES];
[imageData9 writeToFile:savedImagePath9 atomically:YES];
    
[super viewDidLoad];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath1 = [documentsDirectory stringByAppendingPathComponent:@"left.png"];
NSString *savedImagePath2 = [documentsDirectory stringByAppendingPathComponent:@"right.png"];
NSString *savedImagePath3 = [documentsDirectory stringByAppendingPathComponent:@"right3.png"];
NSString *savedImagePath4 = [documentsDirectory stringByAppendingPathComponent:@"up3.png"];
NSString *savedImagePath5 = [documentsDirectory stringByAppendingPathComponent:@"nodeOff.png"];
NSString *savedImagePath6 = [documentsDirectory stringByAppendingPathComponent:@"nodeOn.png"];
NSString *savedImagePath7 = [documentsDirectory stringByAppendingPathComponent:@"webcam002.png"];
NSString *savedImagePath8 = [documentsDirectory stringByAppendingPathComponent:@"webcam003.png"];
NSString *savedImagePath9 = [documentsDirectory stringByAppendingPathComponent:@"plus3.png"];
//also be .jpg or another
UIImage *newImage1 = [UIImage imageNamed:@"left.png"];
UIImage *newImage2 = [UIImage imageNamed:@"right.png"];
UIImage *newImage3 = [UIImage imageNamed:@"right3.png"];
UIImage *newImage4 = [UIImage imageNamed:@"up3.png"];
UIImage *newImage5 = [UIImage imageNamed:@"nodeOff.png"];
UIImage *newImage6 = [UIImage imageNamed:@"nodeOn.png"];
UIImage *newImage7 = [UIImage imageNamed:@"webcam002.png"];
UIImage *newImage8 = [UIImage imageNamed:@"webcam003.png"];
UIImage *newImage9 = [UIImage imageNamed:@"plus3.png"];
NSData *imageData1 = UIImagePNGRepresentation(newImage1);
NSData *imageData2 = UIImagePNGRepresentation(newImage2);
NSData *imageData3 = UIImagePNGRepresentation(newImage3);
NSData *imageData4 = UIImagePNGRepresentation(newImage4);
NSData *imageData5 = UIImagePNGRepresentation(newImage5);
NSData *imageData6 = UIImagePNGRepresentation(newImage6);
NSData *imageData7 = UIImagePNGRepresentation(newImage7);
NSData *imageData8 = UIImagePNGRepresentation(newImage8);
NSData *imageData9 = UIImagePNGRepresentation(newImage9);
//UIImageJPEGRepresentation(image)
[imageData1 writeToFile:savedImagePath1 atomically:YES];
[imageData2 writeToFile:savedImagePath2 atomically:YES];
[imageData3 writeToFile:savedImagePath3 atomically:YES];
[imageData4 writeToFile:savedImagePath4 atomically:YES];
[imageData5 writeToFile:savedImagePath5 atomically:YES];
[imageData6 writeToFile:savedImagePath6 atomically:YES];
[imageData7 writeToFile:savedImagePath7 atomically:YES];
[imageData8 writeToFile:savedImagePath8 atomically:YES];
[imageData9 writeToFile:savedImagePath9 atomically:YES];
1:保存图片以及读取图片的saveImage
 
                     
                    
                 
                    
                 

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号