October 7th, 2010 Posted by: Collin - posted under:Featured » Tutorials
Displaying ALAssets using Asset
Asset is the final class which comprises the ELCImagePickerController. Asset is a UIView subclass which takes in an ALAsset and creates a view that represents it. The final view will be a square thumbnail of the ALAsset that is passed in along with a checkmark overlay that is very similar to the checkmark used in Apple’s Photos application. In AssetCell we set up the UITapGestureRecognizer that will call this method whenever an asset is tapped. ALAssets, which we inject into an Asset object when creating, have a convenience method called thumbnail that we can utilize to get the actual image that will represent the asset. After adding that to the view we add the image view for the overlay and set it as hidden. Our toggleOverlay: method will check whether that view is hidden. Finally we have a checker to see if the asset is selected with the method selected. If the overview is hidden the asset is not selected and if it is visible it is selected. All this comes together to form the final smarts we need to make our picker functional. See the header and main below.
Asset.h
@interface Asset : UIView {
ALAsset *asset;
UIImageView *overlayView;
BOOL selected;
id parent;
}
@property (nonatomic, retain) ALAsset *asset;
@property (nonatomic, assign) id parent;
-(id)initWithAsset:(ALAsset*)_asset;
-(BOOL)selected;
@end
|
Asset.m
@implementation Asset
@synthesize asset;
@synthesize parent;
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Initialization code
}
return self;
}
-(id)initWithAsset:(ALAsset*)_asset {
if (self = [super initWithFrame:CGRectMake(0, 0, 0, 0)]) {
asset = _asset;
[asset retain];
CGRect viewFrames = CGRectMake(0, 0, 75, 75);
UIImageView *assetImageView = [[UIImageView alloc] initWithFrame:viewFrames];
[assetImageView setContentMode:UIViewContentModeScaleToFill];
[assetImageView setImage:[UIImage imageWithCGImage:[asset thumbnail]]];
[self addSubview:assetImageView];
[assetImageView release];
overlayView = [[UIImageView alloc] initWithFrame:viewFrames];
[overlayView setImage:[UIImage imageNamed:@"Overlay.png"]];
[overlayView setHidden:YES];
[self addSubview:overlayView];
}
return self;
}
-(void)toggleSelection {
overlayView.hidden = !overlayView.hidden;
}
-(BOOL)selected {
return !overlayView.hidden;
}
-(void)setSelected:(BOOL)_selected {
[overlayView setHidden:!_selected];
}
- (void)dealloc {
[asset release];
[overlayView release];
[super dealloc];
}
@end
|
Passing the Assets Back Up
So now that all the assets can be displayed and selected lets take a look at how they are passed up the chain. First thing to know is that the ELCImagePickerController passes up Asset objects, since they contain references to the ALAsset which they represent. With that said the place we start is in the AssetTablePicker. When dismiss is clicked there the class calls the dismiss method which looks through the assets array and checks for which one is selected. It adds the selected Asset objects to an array and then calls AlbumPickerControllers selectedAssets: method. The selectedAssets method just passes this array along to the ELCImagePickerController method selectedAssets:. Here a dictionary is constructed to represent every asset that was selected. The dictionary contains the same information that is delivered by UIImagePickerController when it delivers assets. This way the ELCImagePickerController can be included into projects as simply as possible. Below you will see each of the passback methods described above as well as the definition of the ELCImagePickerControllerDelegate Protocol which defines the methods that should be implemented on the calling class.
-(IBAction)dismiss:(id)sender {
NSMutableArray *selectedAssetsImages = [[NSMutableArray alloc] init];
for(Asset *asset in assets) {
if([asset selected]) {
[selectedAssetsImages addObject:[asset asset]];
}
}
[(AlbumPickerController*)parent selectedAssets:[NSArray arrayWithArray:selectedAssetsImages]];
}
|
-(void)selectedAssets:(NSArray*)_assets {
[(ELCImagePickerController*)parent selectedAssets:_assets];
}
|
-(void)selectedAssets:(NSArray*)_assets {
NSMutableArray *returnArray = [[NSMutableArray alloc] init];
for(ALAsset *asset in _assets) {
NSMutableDictionary *workingDictionary = [[NSMutableDictionary alloc] init];
[workingDictionary setObject:[asset valueForProperty:ALAssetPropertyType] forKey:@"UIImagePickerControllerMediaType"];
[workingDictionary setObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]] forKey:@"UIImagePickerControllerOriginalImage"];
[workingDictionary setObject:[[asset valueForProperty:ALAssetPropertyURLs] valueForKey:[[[asset valueForProperty:ALAssetPropertyURLs] allKeys] objectAtIndex:0]] forKey:@"UIImagePickerControllerReferenceURL"];
[returnArray addObject:workingDictionary];
[workingDictionary release];
}
if([delegate respondsToSelector:@selector(elcImagePickerController:didFinishPickingMediaWithInfo:)]) {
[delegate performSelector:@selector(elcImagePickerController:didFinishPickingMediaWithInfo:) withObject:self withObject:[NSArray arrayWithArray:returnArray]];
}
}
|
@protocol ELCImagePickerControllerDelegate
- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info;
- (void)elcImagePickerControllerDidCancel:(ELCImagePickerController *)picker;
@end
|
GIT Hub
You can find this project . Please let me know any issues you may have and look for future releases with feature enhancements. Happy coding!
Follow me on Twitter
Pages: 1 2 3 4