2011-10-18 8 views
1

これは私の問題です.iOS 4.Xでこのクラスにアクセスするとコードはうまく動作します...しかし、iOS 5.0でアクセスしようとすると、グループ&アセットの値。これを動作させる最良の方法は何ですか?私は、私はこれを理解しようとしてきたすべてのヘルプははるかに高く評価されるだろう.MAlAssetsLibraryの問題、コードは4.3では動作しますが、5.0では動作しません

#import <ImageIO/ImageIO.h> 
#import "SelectMediaViewController.h" 
#import "CaptionAllMediaViewController.h" 
#import "MediaItem.h" 
#import "CLValueButton.h" 
#import "SelectMediaTableViewCell.h" 

#define kMediaGridSize 75 
#define kMediaGridPadding 4 

#define kSelectImageTag 828 

@interface SelectMediaViewController(Private) 

- (void)setContentForButton:(CLValueButton *)button withAsset:(ALAsset *)asset; 
- (void)loadData; 

@end 

@implementation SelectMediaViewController 

@synthesize event, venue; 
@synthesize tableView; 
@synthesize allMedia,assetGroups; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
     selectedAssets = [[NSMutableArray alloc] init]; 
     showNextButton = YES; 
    } 
    return self; 
} 

- (void)dealloc { 
    [tableView release];  
    [event release]; 
    [venue release]; 
    [library release]; 
    [allMedia release]; 
    [selectedAssets release]; 
    [assetGroups release]; 

    [super dealloc]; 
} 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    NSLog(@"SelectMediaViewController - viewDidLoad");  
} 

- (void)viewDidUnload { 
    NSLog(@"SelectMediaViewController - viewDidUnload"); 
    [self setTableView:nil]; 
    [super viewDidUnload]; 
} 

- (void)viewDidAppear:(BOOL)animated { 
    NSLog(@"SelectMediaViewController - viewDidAppear"); 
    [super viewDidAppear:animated]; 
    [self setNavTitle:@"Select Media"]; 

    [self loadData]; 

    [self.tableView reloadData]; 
    float contentOffset = self.tableView.contentSize.height - self.tableView.frame.size.height; 
    if (contentOffset < 0) contentOffset = 0; 
    [self.tableView setContentOffset:CGPointMake(0, contentOffset) animated:NO]; 
} 

- (void)viewDidDisappear:(BOOL)animated { 
    NSLog(@"SelectMediaViewController - viewDidDisappear"); 
    self.allMedia = nil; 
    [selectedAssets removeAllObjects]; 
    [self.tableView reloadData]; 
} 

- (void)loadData { 
    NSMutableArray *tempArray = [[NSMutableArray array] init]; 
    library = [[ALAssetsLibrary alloc] init]; 

    void (^assetEnumerator)(ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) { 
     if(result != NULL) { 
      NSLog(@"See Asset: %@", result); 
      [tempArray addObject:result]; 
      NSLog(@"assets count: %i", tempArray.count); 
     } 
     else { 
      NSLog(@"result nil or end of list"); 
     } 
    }; 

    void (^assetGroupEnumerator)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop) { 
     if(group != nil) { 
      [group enumerateAssetsUsingBlock:assetEnumerator]; 
      NSLog(@"group: %@",group); 
     } 
     else { 
      NSLog(@"group nil or end of list"); 
     } 

     if (stop) { 
      self.allMedia = [NSMutableArray arrayWithCapacity:[tempArray count]]; 
      self.allMedia = tempArray; 
      NSLog(@"Loaded data: %d & %d", [tempArray count], [self.allMedia count]); 
     } 
    }; 

    //ALAssetsLibrary *library = [[[ALAssetsLibrary alloc] init] autorelease]; 
    [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos 

          usingBlock:assetGroupEnumerator 
          failureBlock:^(NSError *error) { 

    }]; 

    //[library release]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

- (IBAction)continuePressed:(id)sender { 
    if ([selectedAssets count] > 0) {  
     CaptionAllMediaViewController *captionVC = [[CaptionAllMediaViewController alloc] initWithNibName:nil bundle:nil]; 
     captionVC.event = self.event; 
     captionVC.venue = self.venue; 

     // Create media items 
     NSMutableArray *mediaItems = [NSMutableArray arrayWithCapacity:[selectedAssets count]]; 
     for (ALAsset *asset in selectedAssets) { 
      MediaItem *item = [[MediaItem alloc] init]; 
      item.asset = asset; 

      NSDictionary *metadata = [[asset defaultRepresentation] metadata]; 
      NSDictionary *gpsMeta = [metadata objectForKey:@"{GPS}"]; 
      if (gpsMeta) { 
       float latitude = [[gpsMeta objectForKey:@"Latitude"] floatValue]; 
       if ([[gpsMeta objectForKey:@"LatitudeRef"] isEqualToString:@"S"]) latitude = latitude * -1; 

       float longitude = [[gpsMeta objectForKey:@"Longitude"] floatValue]; 
       if ([[gpsMeta objectForKey:@"LongitudeRef"] isEqualToString:@"W"]) longitude = longitude * -1; 

       item.location = CLLocationCoordinate2DMake(latitude, longitude); 
      } 

      [mediaItems addObject:item]; 
      [item release]; 
     } 

     captionVC.media = mediaItems; 
     [self.navigationController pushViewController:captionVC animated:YES]; 
     [captionVC release]; 
    } else { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Images Selected" 
                 message:@"Please select at least one image to continue." 
                 delegate:nil 
               cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
     [alert show]; 
     [alert release]; 
    } 
} 

- (void)imagePressed:(CLValueButton *)sender { 

    BOOL currentlySelected = [selectedAssets containsObject:sender.valueObject]; 

    UIImageView *imageView = (UIImageView *)[sender viewWithTag:kSelectImageTag]; 
    if (!currentlySelected) { 
     [imageView setImage:[UIImage imageNamed:@"image-select-active.png"]]; 
     [selectedAssets addObject:sender.valueObject]; 
    } else { 
     [imageView setImage:[UIImage imageNamed:@"image-select.png"]]; 
     [selectedAssets removeObject:sender.valueObject]; 
    } 
} 

#pragma Table view methods 

- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    NSLog(@"Getting table view count: %d", [self.allMedia count]); 
    if ([self.allMedia count] == 0) return 0; 
    return ceil([self.allMedia count]/4.0); 
} 

- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return 83; 
    NSLog(@"return83"); 
} 

- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    SelectMediaTableViewCell *cell = (SelectMediaTableViewCell *)[_tableView dequeueReusableCellWithIdentifier:@"MEDIA_CELL"]; 
    if (!cell) { 
     cell = [[[NSBundle mainBundle] loadNibNamed:@"SelectMediaTableViewCell" owner:nil options:nil] objectAtIndex:0]; 
     // wire up selectors 
     [cell.image1 addTarget:self action:@selector(imagePressed:) forControlEvents:UIControlEventTouchUpInside]; 
     [cell.image2 addTarget:self action:@selector(imagePressed:) forControlEvents:UIControlEventTouchUpInside]; 
     [cell.image3 addTarget:self action:@selector(imagePressed:) forControlEvents:UIControlEventTouchUpInside]; 
     [cell.image4 addTarget:self action:@selector(imagePressed:) forControlEvents:UIControlEventTouchUpInside]; 

    } 
    int startIndex = indexPath.row * 4; 

    for (int i = 0; i < 4; i++) { 
     ALAsset *thisAsset = (startIndex + i) < [self.allMedia count] ? [self.allMedia objectAtIndex:startIndex + i] : nil; 

     CLValueButton *button = nil; 
     switch (i) { 
      case 0: 
       button = cell.image1; 
       break; 
      case 1: 
       button = cell.image2; 
       break; 
      case 2: 
       button = cell.image3; 
       break; 
      case 3: 
       button = cell.image4; 
       break; 

      default: 
       break; 

     } 

     [self setContentForButton:button withAsset:thisAsset]; 

     UIImageView *imageView = (UIImageView *)[button viewWithTag:kSelectImageTag]; 
     // letse see if it's selected or not... 
     if ([selectedAssets containsObject:button.valueObject]) { 
      [imageView setImage:[UIImage imageNamed:@"image-select-active.png"]]; 

     } else { 
      [imageView setImage:[UIImage imageNamed:@"image-select.png"]]; 
     } 
    } 
    return cell; 
} 

- (void)setContentForButton:(CLValueButton *)button withAsset:(ALAsset *)asset { 
    button.hidden = asset == nil; 

    if (asset) { 
     CGImageRef image = [asset thumbnail]; 
     [button setImage:[UIImage imageWithCGImage:image] forState:UIControlStateNormal]; 
    } 

    [button setValueObject:asset]; 
} 

#pragma - 

@end 

の.h

#import <UIKit/UIKit.h> 
#import <AssetsLibrary/AssetsLibrary.h> 
#import "DejViewController.h" 

@class Event, Venue; 

@interface SelectMediaViewController : DejViewController <UITableViewDelegate, UITableViewDataSource> { 
    Event *event; 
    Venue *venue; 
    UITableView *tableView; 

    NSMutableArray *selectedAssets; 
    NSMutableArray *allMedia; 
    ALAssetsLibrary *library; 
    NSMutableArray *assetGroups; 
} 

@property (nonatomic, retain) Event *event; 
@property (nonatomic, retain) Venue *venue; 

@property (nonatomic, retain) IBOutlet UITableView *tableView; 
@property (nonatomic, retain) NSMutableArray *allMedia; 
@property (nonatomic, retain) NSMutableArray *assetGroups; 

- (IBAction)continuePressed:(id)sender; 

@end 

...参考のために、クラス全体を掲示しています3日間...

答えて

1

オンラインドキュメントのALAssetsLibraryページでは、「ライブラリインスタンスから取得したオブジェクトのライフタイムは、ライブラリインスタンスの存続期間に関連付けられています」と表示されます。

関連する問題