English 中文(简体)
采购 图书馆问题,代码在4.3但0.5。
原标题:AlAssetsLibrary issue, code works in 4.3 but not 5.0

这里我的问题是,如果我以4.X的手法进入这个类别,那么该法典就会奏效。 怎样才能做到这一点? I m 贴上整个类别,以供参考......

.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天之内看到这种帮助。

问题回答

The ALAssetsLibrary page in the online documentation now says "The lifetimes of objects you get back from a library instance are tied to the lifetime of the library instance."





相关问题
New Notification Center

Is there a way to show a notification in the notification center without having push notifications enabled? For example if an email fails to send is there a way i can create a string and display it in ...

submit an ios app using ios5 OS and ios5 sdk

I m wondering if I can install IOS 5 and the IOS 5 SDK and still be able to develop and submit apps to Apple? Or once I install IOS 5 I m forced to install the IOS5 SDK (from what I understand I can t ...

What s new for web developers in iOS 5 Safari?

From the Apple WWDC we have learned that iOS Safari 5 has tabs, and the new Reader and Reading List functionality. None of which is particularly informative for web developers. What are the new ...

iOS - AudioSession Cateogry for AudioUnit Application

I am working on streaming audio player application for iOS. currently i am experimenting with AudioQueue and AudioUnit for playback. Both works fine in the normal condition. But I am facing an issue ...

Couldn t register with the bootstrap Server

I just changed some code in my program and got this error: Couldn t register com.yourcompany.XXX with the bootstrap server. Error: unknown error code. This generally means that another ...

Unable to manage orientation with multiple view controllers

I m trying to build an iPhone application that has two subviews in the main window. Each view has its own UIViewController subclass associated with it. Also, within each controller s implementation, I ...

热门标签