iphone 儲存圖片以及讀取圖片 demo

來源:互聯網
上載者:User

 顯示圖片:思路:想利用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];
    

 

  

 

 儲存圖片:思路,先將圖片儲存為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];

    

 

 

1:儲存圖片以及讀取圖片的saveImage

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.