I want to save an image that is displayed in the UIImage
. However I want to categorize it in genre for example vegetables, meat, lettuce, etc.
I cannot find a way to do that.
What is the best and easiest way to do this?
Also I want to make the user select which category he wants to save using the picker controller to save. Thank you! I really need help!!
Update 2
To check if it is saved I went further to construct a collection View controller and I get errors saying at the images part at the [arrayCollectionImages addObject:image];
Can you explain me about this last piece of code. I also have an warning at arrayCollectionImages saying local declaration of arrayColectionImages hide instance variable. –
@interface CollectionViewController (){
NSArray *arrayCollectionImages;
}
@end
@implementation CollectionViewController
- (void)viewDidLoad {
dispatch_queue_t searchQ = dispatch_queue_create("com.awesome", 0);
dispatch_async(searchQ, ^{
NSArray *arrayCollectionImages = [[NSArray alloc ]init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *location=@"Hats";
NSString *fPath = [documentsDirectory stringByAppendingPathComponent:location];
NSArray *directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath: fPath];
for(NSString *str in directoryContent){
NSString *finalFilePath = [fPath stringByAppendingPathComponent:str];
NSData *data = [NSData dataWithContentsOfFile:finalFilePath];
if(data)
{ dispatch_async(dispatch_get_main_queue(),^{
UIImage *image = [UIImage imageWithData:data];
});
[arrayCollectionImages addObject:images];
}
}
});
[super viewDidLoad];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ReuseID" forIndexPath:indexPath];
[[cell collectionImageView]setImage:[UIImage imageNamed:[arrayCollectionImages objectAtindex:indexPath.item]]];
return cell;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return [arrayCollectionImages count];
}
@end
Step 1:
Set up a Picker Controller.
Here is the official reference.
Here is a tutorial.
Try to set it with all the genre values you want. You will learn about its delegate method:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
that will return the genre selected.
Step 2:
Provide a save icon or a save button on each image. On click of that icon/button you should make your picker view appear and make user select any row and the image should be copied to a private variable or a property here, so as to refer it later in the delegate method.
Step 3: When user selects a row, this delegate method is called. Here we will create a new directory , if not already exists, according to the genre selected In UIPickerView. Then you should write your image to the path.
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//fetch Category Name from the array used to fill the Picker View
NSString *categoryName= [array objectAtIndex:row];
NSString *fPath = [documentsDirectory stringByAppendingPathComponent:categoryName];
NSFileManager *fileManager=[[NSFileManager alloc]init];
[fileManager createDirectoryAtPath:fPath withIntermediateDirectories:YES attributes:NO error:NO];
UIImage *image= captureImage;
NSData *data= UIImagePNGRepresentation(image);
[data writeToFile:fPath atomically:NO];
}
To retrieve images from a particular folder in your documents directory:
Create a path to that folder first. This will depend on a particular genre. Let's say Genre1.
// An array to save all images. You have to do this in some other way if selected images are big in size.
// For that case I suggest retrieving of images only when you are showing them on screen.
NSArray *allImagesArray = [[NSArray alloc ]init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *location=@"Genre1";
NSString *fPath = [documentsDirectory stringByAppendingPathComponent:location];
NSArray *directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath: fPath];
for(NSString *str in directoryContent){
NSString *finalFilePath = [fPath stringByAppendingPathComponent:str];
NSData *data = [NSData dataWithContentsOfFile:finalFilePath];
if(data)
{
UIImage *image = [UIImage imageWithData:data];
[allImagesArray addObject:image];
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.