访问应用中保存的新照片

时间:2015-11-18 11:16:52

标签: ios objective-c iphone

这是我的第一个问题,如果我犯了任何错误,那就很抱歉。

我基本上有一个Objective-C应用程序:显示之前拍摄的照片,拍摄并保存新照片。

在我的第一个视图中,我将此代码用于显示集合视图中的早期照片:

AnalysisException: Reference 'a' is ambiguous, could be: a#1333L, a#1335L.

-(void)viewDidLoad {
    [super viewDidLoad];

    paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    documentsDirectory = [paths objectAtIndex:0];
    imagePaths = [[NSBundle bundleWithPath:documentsDirectory] pathsForResourcesOfType:@"png" inDirectory:nil];
    imageCount = imagePaths.count;
}

使用以下方法创建集合视图:

-(void)viewWillAppear:(BOOL)animated{    
    paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    documentsDirectory = [paths objectAtIndex:0];
    imagePaths = [[NSBundle bundleWithPath:documentsDirectory] pathsForResourcesOfType:@"png" inDirectory:nil];
    imageCount = imagePaths.count;
    //NSLog([NSString stringWithFormat:@"%d",imageCount]);
    [_collectionView reloadData];
}

在另一个视图中,我保存拍摄的照片,我有这个:

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return imageCount;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PictureCollectionViewCell *cell = (PictureCollectionViewCell *) [_collectionView dequeueReusableCellWithReuseIdentifier:@"PictureCell" forIndexPath:indexPath];
    UIImage *image = [UIImage imageWithContentsOfFile:[imagePaths objectAtIndex:indexPath.row ]];

    cell.imageView.image = image;
    return cell;
}


-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(80, 80);
}

我的问题是,第一个视图在应用首次启动时成功显示早期的照片。但拍完一张新照片后,保存并返回第一个视图; -(void) savePhotoMethod{ NSData* imageData = UIImagePNGRepresentation(imageView.image); NSString *fileName = [NSString stringWithFormat:@"%d",photoIndex ]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *imagePath =[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",fileName]]; NSFileManager *fileManager = [NSFileManager defaultManager]; [fileManager createFileAtPath:imagePath contents:imageData attributes:nil]; /*if ([fileManager fileExistsAtPath:imagePath]) { NSLog(@"succes!!"); NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSArray *imagePaths = [[NSBundle bundleWithPath:documentsDirectory] pathsForResourcesOfType:@"png" inDirectory:nil]; int imageCount = imagePaths.count; NSLog([NSString stringWithFormat:@"%d",imageCount]); } I also have his control, so I know the photo is actually saved successfully, but the imageCount is still showing me the number like last photo is not counted.*/ [self.navigationController popToRootViewControllerAnimated:YES]; } viewDidLoad方法无法找到上次保存的照片。虽然当我退出应用并再次重新启动它时,我也能看到最后的照片。

欢迎任何有关此问题或想法的建议,以提高我的应用效率!

2 个答案:

答案 0 :(得分:0)

尝试以下代码

.pt-page-moveFromRight {
            -webkit-animation: moveFromRight .6s ease both;
            animation: moveFromRight .6s ease both;
        }
        @-webkit-keyframes moveFromRight {
            from { -webkit-transform: translateX(100%); }
        }
        @keyframes moveFromRight {
            from { -webkit-transform: translateX(100%); transform: translateX(100%); }
        }

编辑viewwillAppear方法

-(void) savePhotoMethod
{   
    dispatch_queue_t mainThreadQueue = dispatch_get_main_queue();
    dispatch_async(mainThreadQueue, ^{

       NSData* imageData = UIImagePNGRepresentation(imageView.image);
       NSString *fileName = [NSString stringWithFormat:@"%d",photoIndex ];
       NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
       NSString *documentsDirectory = [paths objectAtIndex:0];
       NSString *imagePath =[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",fileName]];

       NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager createFileAtPath:imagePath contents:imageData attributes:nil];

       [self.navigationController popToRootViewControllerAnimated:YES];

    });

}

答案 1 :(得分:0)

你可以这样试试吗:

-(void) savePhotoMethod
{
NSOperationQueue *opQueue = [[NSOperationQueue alloc] init];
[opQueue addOperationWithBlock:^
 {
 NSData* imageData = UIImagePNGRepresentation(imageView.image);
   NSString *fileName = [NSString stringWithFormat:@"%d",photoIndex ];
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *documentsDirectory = [paths objectAtIndex:0];
   NSString *imagePath =[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",fileName]];

   NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager createFileAtPath:imagePath contents:imageData attributes:nil];
 // ok, now do UI stuff in the main queue

 [[NSOperationQueue mainQueue] addOperationWithBlock:^
  {
      [self.navigationController popToRootViewControllerAnimated:YES];
  }];

}];     }

希望它能解决您的问题