AlAssetsLibrary 问题,代码适用于 4.3 但不适用于 5.0
Posted
技术标签:
【中文标题】AlAssetsLibrary 问题,代码适用于 4.3 但不适用于 5.0【英文标题】:AlAssetsLibrary issue, code works in 4.3 but not 5.0 【发布时间】:2011-10-18 05:31:49 【问题描述】:这是我的问题,如果我使用 ios 4.X 访问这个类,代码可以正常工作....但是每当我尝试使用 iOS 5.0 访问它时,我得到的组和资产的值为 nil。让它发挥作用的最佳方法是什么?我正在发布整个课程以供参考...
.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
.m
#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
任何帮助将不胜感激,我已经尝试了 3 天...
【问题讨论】:
【参考方案1】:在线文档中的 ALAssetsLibrary 页面现在显示“从库实例返回的对象的生命周期与库实例的生命周期相关联。”
【讨论】:
以上是关于AlAssetsLibrary 问题,代码适用于 4.3 但不适用于 5.0的主要内容,如果未能解决你的问题,请参考以下文章
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; - 错误