I want to make something like a favorite button for an iPhone camera roll.

Asked 2 years ago, Updated 2 years ago, 90 views

I would like to make something like a favorite button (heart-shaped button) for iPhone camera roll.
The photos I took are placed in the collection view, and when I tap on one of them, the screen transitions to create a camera roll that I can see full of the screen.

I'd like to recognize the favorite button for each image by the BOOL value, but I don't know how to save the BOOL value for each image.

I would like to get an array of images in the MainViewController segment and put a BOOL value in the array of images obtained in DetailViewController. Is it possible to put two images and a BOOL value in one NSMutableArray array?

Please let me know if anyone knows anything.

MainViewController.m

 - (void) prepareForSege: (UIStoryboardSege*) segue sender: (id) sender
{
    if([segue identifier] isEqualToString:@"showDetailView")
    {
        NSIndexPath*indexPath=[self.collectionView.indexPathsForSelectedItems objectAtIndex:0];

        UICollectionViewCell*cell=[self.collectionViewcellForItemAtIndexPath:indexPath];

        UIImageView* selectedImage=(UIImageView*) [cell viewWithTag:1];

        // screen transition
        ShowViewController* showViewController=segue.destinationViewController;

        showViewController.image2 = selectedImage.image;

        showViewController.delegate=self;


        // Specify where tapped

        // Identify CollectionHeaderView
        UIView* view=sender;
        while(view&&![view isKindOfClass: CollectionHeaderView class]]){
            view=view.superview;
        }
        CollectionHeaderView* headerView=(CollectionHeaderView*)view;

        //_imageSelectionIndexPath specifies the currently selected section
        _imageSelectionIndexPath=headerView.indexPath;

        // Set the initial value of the Favorites button
        // Get the BOOL value for each image displayed here
    }
} 

DetailViewController.m

 - (void) viewDidLoad
{
    if(self.favoriteButton){
        NSLog(@"Favorite button BOOL is YES");      
    } else{
        NSLog (@ "BOOL on Favorite Button is NO");        
    }
}

-(void)tappedFavorite
{
    // Save BOOL value here
}

Lesson.h

#import<Foundation/Foundation.h>
# import "Record.h"

@ interface Lesson —NSObject <NSCoding >

@property(nonatomic)NSString*name;
@property(nonatomic)NSString*teacher;
@property(nonatomic)NSString*room;
@property(nonatomic)NSMutableArray* records;

+ (NSArray*) fetchLessons;
+ (void) saveLessons: (NSArray*)lessons;

@end

Lesson.m

#import "Lesson.h"

@implementation Lesson

+ (NSArray*) fetchLessons
{
    NSSstring* path = [self makeLibraryPath];

    NSData*data=[NSData alloc] initWithContentsOfFile:path];

    NSArray*array= [NSKeyedUnarchiver unarchiveObjectWithData];

    if(!array){
        array = [NSArray array];
    }
    return array;
}


+ (void) saveLessons: (NSArray*)lessons
{
    NSData*data=[NSKeyedArchiver archivedDataWithRootObject:lessons];

    NSSstring* path = [self makeLibraryPath];

    if([data writeToFile:pathatomically:YES]) {
        NSLog (@ "Picture Saved Successfully");
    } else{
        NSLog (@ "Picture Save Failed");
    }   
}


+ (NSString*) makeLibraryPath
{
    NSArray* paths = NSSearchPathForDirectoriesInDomains (NSLibraryDirectory, NSUserDomainMask, YES);

    NSSstring*dir = [paths objectAtIndex:0];

    return [dir stringByAppendingPathComponent:@"data.dat"; 
}

- (instancetype) init
{
    self=[super init];
    if(self){
        self.records = NSMutableArray array;
    }
    return self;
}

- (id) initWithCoder: (NSCoder*) aDecoder
{
    self=[super init];
    if(self){
        self.name = [aDecoder decodeObjectForKey:@"name";
        self.teacher=[aDecoder decodeObjectForKey:@"teacher";
        self.room=[aDecoder decodeObjectForKey:@"room";
        self.records = [aDecoder decodeObjectForKey:@"records";

    //self.favButton=[aDecoder decodeObjectForKey:@"favorite";
    }
    return self;
}

- (void)encodeWithCoder: (NSCoder*)aCoder
{
    [aCoder encodeObject: self.name forKey:@"name";
    [aCoder encodeObject:self.teacher forKey:@"teacher";
    [aCoder encodeObject:self.room forKey:@"room";
    [aCoder encodeObject:self.records forKey:@"records";
}

@end

Record.h

#import<Foundation/Foundation.h>

@ interface Record:NSObject <NSCoding >

@property(nonatomic)NSDate*date;
@property(nonatomic)NSMutableArray*images;
@end

Record.m

#import "Record.h"

@implementation Record

- (instancetype) init
{
    self=[super init];
    if(self){
        self.images = NSMutableArray array;
    }
    return self;
}

- (id) initWithCoder: (NSCoder*) aDecoder
{
    self=[super init];
    if(self){
        self.date = [aDecoder decodeObjectForKey:@"date";
        self.images=[aDecoder decodeObjectForKey:@"images";

    }
    return self;
}

- (void)encodeWithCoder: (NSCoder*)aCoder
{
    [aCoder encodeObject:self.date forKey:@"date";
    [aCoder encodeObject:self.images forKey:@"images";
}

@end

objective-c xcode iphone

2022-09-30 19:16

1 Answers

Since BOOL values cannot be stored in the image, you can save the image name (Key) and favorite (BOOL) in NSUserDefaults and switch the favorite ON/PODictionary every time the image is called.


2022-09-30 19:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.