0

I am developing one app in that getting images from array and display vertically in ScrollView.

when user double tapped on particular image i want that exact image store into plist according to tag value of that image, and retrieve that image later on when require.

i tried this one

//  Store Data into plist.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask,
                                                         YES);
    NSString *path = [NSString stringWithFormat:@"%@/myPlist.plist",
                      [paths objectAtIndex:0]];

    // Place an image in a dictionary that will be stored as a plist

    NSMutableDictionary * dictionary=[[NSMutableDictionary alloc]init];


    [dictionary setObject:ImgView.tag forKey:@"image"];

    NSLog(@"%@",dictionary);

    // Write the dictionary to the filesystem as a plist
    [NSKeyedArchiver archiveRootObject:dictionary toFile:path];

// For getting data from NSmutable array store it to the scrollview.

int m=0;

AppDelegate * delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];

delegate.front=TRUE;
delegate.back=FALSE;

UIScrollView *scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];

[scrollView setPagingEnabled:YES];

[scrollView setShowsHorizontalScrollIndicator:NO];

FrontsCards=[[NSMutableArray alloc]initWithObjects:@"cloub1.png",@"cloub2.png",@"cloub3.png",@"cloub4.png",@"cloub5.png",@"cloub6.png",@"cloub7.png",@"cloub8.png",@"cloub9.png",@"cloub10.png",@"cloub11.png",@"cloub12.png",@"diamond1.png",@"diamond2.png",@"diamond3.png",@"diamond4.png",@"diamond5.png", nil];




for(m=0; m<[FrontsCards count];m++)
{

    ImgView.alpha=1;

    ImgView.tag=m;

    int randIdx=arc4random()%[FrontsCards count];

    NSString *imageName=[FrontsCards objectAtIndex:randIdx];

    NSString *fullImageName=[NSString stringWithFormat:@"%@",imageName];

    int padding=0;

    CGRect imageViewFrame=CGRectMake(scrollView.frame.size.width*m+padding, scrollView.frame.origin.y, scrollView.frame.size.width-2*padding, scrollView.frame.size.height);

    ImgView=[[UIImageView alloc]initWithFrame:imageViewFrame];

    [ImgView setImage:[UIImage imageNamed:fullImageName]];




    NSLog(@"%d",m);

    // Place an image in a dictionary that will be stored as a plist
    //[dictionary setObject:image forKey:@"image"];

    // Write the dictionary to the filesystem as a plist
    //[NSKeyedArchiver archiveRootObject:dictionary toFile:path];



    [scrollView addSubview:ImgView];


    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapImgView:)];
    doubleTap.numberOfTapsRequired = 2;
    doubleTap.delegate = self;

    [self.ImgView addGestureRecognizer:doubleTap];

    self.ImgView.userInteractionEnabled=YES;

}

CGSize scrollViewSize=CGSizeMake(scrollView.frame.size.width*[FrontsCards count], scrollView.frame.size.height);
[scrollView setContentSize:scrollViewSize];
[self.view addSubview:scrollView];

help me out this thanks in advance.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Jitendra
  • 5,055
  • 2
  • 22
  • 42
  • What is your question? – GayleDDS May 18 '13 at 05:21
  • i want save doubble tapped image into plist. – Jitendra May 18 '13 at 05:22
  • see this http://stackoverflow.com/questions/10846620/ios-load-image-from-plist-file http://stackoverflow.com/questions/2486705/storing-image-in-plist – SAMIR RATHOD May 18 '13 at 05:28
  • my requirement is differant. i want store only dobuble tapped image into plist. – Jitendra May 18 '13 at 05:34
  • what is double tapped image here – Prince Kumar Sharma May 18 '13 at 05:40
  • check my code displaying 15 images vertically and user tapped on any image i want to get that image and store it into plist how may i do this help me out this. – Jitendra May 18 '13 at 05:50
  • Use tap Gesture Recognizer . Add tap gesture recognizer for each image.. Set its Tap property is 2 .... – Kalpesh May 18 '13 at 06:21
  • doing all this things if anyone knows how to save doubble tapped image into plist help is appreciated. – Jitendra May 18 '13 at 06:30
  • @JitendraDeore as your code states, you are storing `Tag` of image not `UIImage` in plist. – Dipen Panchasara May 18 '13 at 06:40
  • @Dipen so how to store image in plist with respect to the Tag – Jitendra May 18 '13 at 06:55
  • @JitendraDeore i have posted code which will help you to save and retrieve image from plist. to get particular image tag wise use `FrontsCards[recognizer.view.tag]` in your gesture delegate method – Dipen Panchasara May 18 '13 at 07:00
  • @Dipen thanks i am tried this one.but that al my image randomly shuffle it will not get the exact image. – Jitendra May 18 '13 at 07:10
  • which one is shuffled? in array? use `NSMutableOrderSet` to preserve the order of your images `or` read image with that tag number from bundle `[UIImage imageNamed:[NSString stringWithFormat:@"cloub%d.png",recognizer.view.tag+1]]`, that thing you need to manage yourself, i have given solution to store and retrieve image from and to plist. – Dipen Panchasara May 18 '13 at 07:15

3 Answers3

1

Define this MACRO Definition at the top of your .m file

#define LIB_DIR_PATH    NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0]

Use this function to Save Image to Plist with Image and Name

- (void)saveImage:(UIImage *)image WithName:(NSString *)imageName
{
    // If File Exist then read it otherwise creat new
    NSMutableDictionary *imageInfoDict;
    if([[NSFileManager defaultManager] fileExistsAtPath:[LIB_DIR_PATH stringByAppendingPathComponent:@"imageInfo.plist"]])
    {
        NSData *fileData = [NSData dataWithContentsOfFile:[LIB_DIR_PATH stringByAppendingPathComponent:@"imageInfo.plist"]];
        imageInfoDict = [NSMutableDictionary dictionaryWithDictionary:[NSKeyedUnarchiver unarchiveObjectWithData:fileData]];
    }
    else
        imageInfoDict = [NSMutableDictionary dictionaryWithCapacity:0];

    // Add Single Image to Dictionary
    [imageInfoDict setValue:image forKey:imageName];

    // Convert Main info Dictionary to `NSData` to Save on Disc
    [NSKeyedArchiver archiveRootObject:imageInfoDict toFile:[LIB_DIR_PATH stringByAppendingPathComponent:@"imageInfo.plist"]];

    // To Read Stored Image Use Following Code
    [self readImageFromPlistByKey:imageName];
}

This function returns image for respective name from Plist

-(UIImage *)readImageFromPlistByKey:(NSString *)keyName
{
    // If File Exist then read it otherwise creat new
    NSMutableDictionary *imageInfoDict;
    if([[NSFileManager defaultManager] fileExistsAtPath:[LIB_DIR_PATH stringByAppendingPathComponent:@"imageInfo.plist"]])
    {
        NSData *fileData = [NSData dataWithContentsOfFile:[LIB_DIR_PATH stringByAppendingPathComponent:@"imageInfo.plist"]];
        if([fileData length] > 0)
        {
            // Read Plist
            imageInfoDict = [NSMutableDictionary dictionaryWithDictionary:[NSKeyedUnarchiver unarchiveObjectWithData:fileData]];

            // Here is your Image
            return imageInfoDict[keyName];
        }
    }
    else
    {
        // Return Default Image if not Found
        return [UIImage imageNamed:@"Default.png"];
    }
}
Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57
1

If you are going to just store indexes, you need to have a master imageArray. I added insert/delete when user double taps the imageView twice.

- (void)doubleTapImgView:(UITapGestureRecognizer *)recognizer
{
    UIImageView *imageView = (UIImageView *)recognizer.view;
    [self insertorDeleteImageIndex:imageView.tag-1];

}

- (NSString *)plistFilePath{
    NSString *documents =  NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    return [documents stringByAppendingPathComponent:@"ImageIndexes.plist"];
}

- (void)insertorDeleteImageIndex:(NSInteger)index{

    NSString *filePath = [self plistFilePath];
    NSMutableArray *savedIndexes = [NSMutableArray arrayWithContentsOfFile:filePath];
    if (!savedIndexes) {
        savedIndexes = [NSMutableArray array];
    }

    if (![savedIndexes containsObject:@(index)]) {
        [savedIndexes addObject:@(index)];
    }else{
        [savedIndexes removeObject:@(index)];
    }

    [savedIndexes writeToFile:filePath atomically:YES];

}

- (NSArray *)savedImageIndexes{
    NSString *filePath = [self plistFilePath];
    return [NSArray arrayWithContentsOfFile:filePath];
}

Source code

Anupdas
  • 10,211
  • 2
  • 35
  • 60
  • @JitendraDeore I have included source code i worked out for you. See where you are going wrong. – Anupdas May 18 '13 at 07:12
  • @JitendraDeore I think i forgot to include that in my code. Please add your resources and remove the hardcoded image from my code. It should work. – Anupdas May 18 '13 at 07:24
0

The code you post above can't be the real code as it wouldn't compile. That said, it shows a few errors:

  1. You can't put basic numbers (NSInteger) into a dictionary, it needs to be boxed in an NSNumber.
  2. You're setting the tag of the image before you create the instance of the image view (so either it will do nothing or set the wrong tag).

For saving the image, if you do want to save the image instead of the tag, you need to save it as data. You can store an image inside a dictionary no problem, but if you then want to store your dictionary as a plist you need to convert the image to NSData. You can get the image data using:

UIImageJPEGRepresentation(imageToSave, 0.8)
Wain
  • 118,658
  • 15
  • 128
  • 151