将图像保存到相册一次

时间:2014-07-08 08:30:01

标签: ios objective-c uiimage boolean

我在文档文件夹中有图像并获取图像然后保存到自定义相册。一旦图像存储到相册,我就使用BOOL标志NO。但是当我启动应用程序时,它不会存储到相册中。

viewController.h:

@property (nonatomic) BOOL savealbum;

viewController.m:

- (void)viewDidLoad
{
    if (_savealbum) {
        [self savePhoto];
    }
}

- (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:@"Room Template" withCompletionBlock:^(NSError *error) {

            _savealbum = NO;

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

2 个答案:

答案 0 :(得分: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]);
                }
            }];
        }
}

答案 1 :(得分:0)

AppDelegate.h
@interface AppDelegate : UIResponder <UIApplicationDelegate,UIAlertViewDelegate>

{
    BOOL isFirst;
}
@property(nonatomic,assign)BOOL isFirst;
- (BOOL) isFirstRun;

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    NSLog(@"isfirst:%d",[self isFirstRun]);
    }
- (BOOL) isFirstRun
{
    Defaults = [NSUserDefaults standardUserDefaults];
    if ([Defaults objectForKey:@"savealbum"])
    {
        //if application running in second time
        isFirst=NO;
        NSLog(@"isSecondRun:%d",isFirst);
        return NO;
    }
    //if application running in first time
    isFirst=YES;
    NSLog(@"isFirstRun:%d",isFirst);

    [Defaults setObject:[NSDate date] forKey:@"savealbum"];
    [[NSUserDefaults standardUserDefaults] synchronize];

    return YES;
}

在viewController.m中:

if(appDelegate.isFirst==1)
{
[self savePhoto];
}

-(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) {
                   appDelegate.isFirst=NO;
                if (error!=nil) {
                    NSLog(@"Big error: %@", [error description]);
                }
            }];
        }
}

希望它可以帮助