2016-10-21 98 views
-1

enter image description here採摘圖像如何IOS

本地保存圖像時,我捕捉我需要一個一個表視圖保存相似圖片圖像。就是下面的圖片後,需要使用nsuser默認設置或使用核心數據? 和採摘圖像如何添加到陣列

+0

只需保存圖像在目錄中唯一的名稱,商店的獨特的名稱爲數組,並從目錄檢索圖像,顯示在你的tableview中。如果你想答案然後我會發布它 –

+0

如果你想保存圖像的默認照片庫,然後使用.. https://stackoverflow.com/a/44328085/5315917 –

回答

0

是的,你可以在文件目錄中保存的圖像,並保留圖像文件名在後之後的數組從doc檢索圖像。 DIR。就像這樣

// For error information 
    NSError *error; 
    NSFileManager *fileMgr = [NSFileManager defaultManager]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder 
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/YOUR_IMG_FOLDER"]; 

    if (![fileMgr fileExistsAtPath:dataPath]) 
     [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder 

    //Get the current date and time and set as image name 
    NSDate *now = [NSDate date]; 

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    dateFormatter.dateFormat = @"yyyy-MM-dd_HH-mm-ss"; 
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]]; 
    NSString *gmtTime = [dateFormatter stringFromDate:now]; 
    NSLog(@"The Current Time is :%@", gmtTime); 

    NSData *imageData = UIImageJPEGRepresentation(_postImage, 0.5); // _postImage is your image file and you can use JPEG representation or PNG as your wish 
    int imgSize = imageData.length; 
    ////NSLog(@"SIZE OF IMAGE: %.2f Kb", (float)imgSize/1024); 

    NSString *imgfileName = [NSString stringWithFormat:@"%@%@", gmtTime, @".jpg"]; 
    // File we want to create in the documents directory 
    NSString *imgfilePath= [dataPath stringByAppendingPathComponent:imgfileName]; 
    // Write the file 
    [imageData writeToFile:imgfilePath atomically:YES]; 

    **// Keep your Image file name into an mutable array here OR you can saved the array in a UserDefaults** 

然後從文檔中檢索。 DIR。從迭代數組。

//Get image file from sand box using file name and file path 
NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"YOUR_IMG_FOLDER"]; 
stringPath = [stringPath stringByAppendingPathComponent:imgFile]; // imgFile to get from your array, where you saved those image file names 
UIImage *image = [UIImage imageWithContentsOfFile:stringPath]; 

謝謝你......快樂的編碼。

+0

謝謝你作爲答覆 –

+0

你可以解釋一下,請不要混淆如何添加圖像一個一個地排列如何顯示在桌面視圖 –

+0

你是捕捉圖像或從服務器獲取??? –

1

您可以保存圖像NSUserDefaults的如下,

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    UIImage *chosenImage = info[UIImagePickerControllerEditedImage]; 
    if(!self.phto_arr_) 
    { 
    self.phto_arr_ = [[NSMutableArray alloc] init]; 
    } 
[self.phto_arr_ addObject: chosenImage]; 
NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:self.phto_arr_]; 
[[NSUserDefaults standardUserDefaults] setObject: encodedObject forKey:@"images"]; 
[[NSUserDefaults standardUserDefaults] synchronize]; 
[picker dismissViewControllerAnimated:YES completion:NULL]; 
} 
+0

謝謝你的重播 –

+0

你是最歡迎 –

0

嘗試下面這段代碼: 假設你要像名字也顯示使用此代碼,

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ 
    chosenImage = info[UIImagePickerControllerOriginalImage]; 
    chosenImage = [UIImage unrotateImage:chosenImage];  
    addGalleryImageview.image = chosenImage; 
    isCameraOn = YES;  
    NSString* fileName = [NSString stringWithFormat:@"gallery%@",[Utils getDateString]]; 
    imageNamelbl.text = fileName; 
    [picker dismissViewControllerAnimated:YES completion:NULL];} 


- (UIImage*)unrotateImage:(UIImage*)image{ 
    CGSize size = image.size; 
    UIGraphicsBeginImageContext(size); 
    [image drawInRect:CGRectMake(0,0,size.width ,size.height)]; 
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return newImage;}