停止重复将图像存储到相册中

时间:2014-06-11 08:39:59

标签: ios uiimage nsarray

我正在将图像存储到相册中。我使用数组获取图像并存储到相册。但是当我每次都存放它的应用程序时。因此,图像重复存储。如果图像存在于相册中,如何停止存储图像

NSArray *photos = [NSArray arrayWithObjects:
                   [UIImage imageNamed:@"img.jpg"],
                   [UIImage imageNamed:@"img1.jpg"],
                   [UIImage imageNamed:@"img2.jpg"],
                   nil] ;



for(int i=0; i<[photos count]; i++){


      image1=[photos objectAtIndex:i];


    NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
    BOOL isSaved=[[NSUserDefaults standardUserDefaults]boolForKey:@"photosave"];
    if (!isSaved) {
        // Save photo

        [self.library saveImage:image1 toAlbum:@"Art" withCompletionBlock:^(NSError *error) {
            //   arappdelegate.isNotSaved=FALSE;
            if (error!=nil) {
                NSLog(@"Big error: %@", [error description]);
            }
        }];
        [userDefaults setBool:YES forKey:@"photosave"];
        [userDefaults synchronize];
    }

}

2 个答案:

答案 0 :(得分:0)

    There is way like use any boolean variable in AppDelegate..



     Appdelegate.h
        @property(nonatomic,readwrite)BOOL isNotSaved;

        Appdelegate.m
        @synthesize isSaved;

        -applicationLaunchMethod
        {
             isNotSaved=TRUE;
        }

        in YourClass.m
        -(void)ViewDidLoad
        {
          appDelegate=[[UIApplication sharedApplication]delegate];
        }

        -(void)YourMethod
        {
        NSArray *photos = [NSArray arrayWithObjects:
                               [UIImage imageNamed:@"img.jpg"],
                               [UIImage imageNamed:@"img1.jpg"],
                               [UIImage imageNamed:@"img2.jpg"],
                               nil] ;

       if(appDelegate.isNotSaved)
    {
        for(int i=0; i<[photos count]; i++){

            image1=[photos objectAtIndex:i];

            [self.library saveImage:image1 toAlbum:@"Art" withCompletionBlock:^(NSError *error)

             {

                 if (error!=nil) {
                     NSLog(@"Big error: %@", [error description]);
                 }
             }];
        }

        appDelegate.isNotSaved=FALSE;
    }
}

答案 1 :(得分:0)

NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
BOOL isSaved=[[NSUserDefaults standardUserDefaults]boolForKey:@"photosave"];
if (!isSaved) {
            [self savePhoto];
    [userDefaults setBool:YES forKey:@"photosave"];
    [userDefaults synchronize];
}

//

-(void)savePhoto{
NSArray *photos = [NSArray arrayWithObjects:
                   [UIImage imageNamed:@"img.jpg"],
                   [UIImage imageNamed:@"img1.jpg"],
                   [UIImage imageNamed:@"img2.jpg"],
                   nil] ;
for(int i=0; i<[photos count]; i++){
        image1=[photos objectAtIndex:i];
        [self.library saveImage:image1 toAlbum:@"Art" withCompletionBlock:^(NSError *error) {
            //   arappdelegate.isNotSaved=FALSE;
            if (error!=nil) {
                NSLog(@"Big error: %@", [error description]);
            }
        }];
    }

}