iPhone: How to get Video file binary when using ALAssetsLibrary enumeration? - iphone

I am trying to access iPhone's photo album gallery through ALAssetsLibrary. I am successful accessing the photo album and get the asset URL of each images on the device. My testing is, after getting all the images, uploading to my web server is also working well.
I am also trying to get the video file and do the same process, i.e. upload video file to my server. Here is the issue, i don't know how to access video pointer binary like how i did for images using UIImageJPEGRepresentation, and send to my HTTP code?
-(void) assetsEmumeration {
void (^assetEnumerator)(struct ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result != NULL) {
NSLog(#"See Asset: %#", result);
[assetArray addObject:result];
// get image url
NSURL *url = [[result defaultRepresentation] url];
NSString *urlInStr = [NSString stringWithFormat:#"%#",url];
NSData *imgData = UIImageJPEGRepresentation ([UIImage imageWithCGImage:[result thumbnail]], 90);
HttpUpload (imgData);
}
};
void (^assetGroupEnumerator)(struct ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop)
{
if(group != nil) {
[group enumerateAssetsUsingBlock:assetEnumerator];
}
[self.activity stopAnimating];
[self.activity setHidden:YES];
};
assetLibrary = [[ALAssetsLibrary alloc] init];
[assetLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
usingBlock:assetGroupEnumerator
failureBlock: ^(NSError *error) {
NSLog(#"Failure: %#", [error localizedDescription]);
}];
assetArray = [[NSMutableArray array] retain];
}
Could someone came across this to solve, please guide me!

You can get the raw bytes using ALAssetRepresentation's getBytes:fromOffset:length:error: method, something like this:
uint8_t *bytes = malloc([representation size]);
NSError *error = nil;
NSUInteger length = [representation getBytes:bytes fromOffset:0 length:[representation size] error:&error];

Related

how to get total count of photos from iphone photoLibrary.?

i want to get the count of photos in photoLibrary. Currently, i'am able to get the photoes from photoLibrary and add to myApp's Document directry ONE BY ONE. But what i want is, save all the photos from photoLibrary to Document directry of myApp ALL AT ONCE. Thats why i need the count of photoes in photoLibrary. I've used UIImagePickerControllerSourceTypePhotoLibrary to retrieve the photoes from iPhone photoLibrary.
Any help would be appreaciated..
thanks in advance....
Use ALAssetsLibrary for this:
int imgCount = 0;
self.assetsLibrary = [[ALAssetsLibrary alloc] init];
dispatch_queue_t dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(dispatchQueue, ^(void) {
[self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index,
__block BOOL foundThePhoto = NO;
if (foundThePhoto){ *stop = YES;
}
BOOL *stop) {
/* Get the asset type */
NSString *assetType = [result valueForProperty:ALAssetPropertyType];
if ([assetType isEqualToString:ALAssetTypePhoto]){ NSLog(#"This is a photo asset");
foundThePhoto = YES; *stop = YES;
/* Get the asset's representation object */
ALAssetRepresentation *assetRepresentation = [result defaultRepresentation];
/* We need the scale and orientation to be able to construct a properly oriented and scaled UIImage out of the representation object */
CGFloat imageScale = [assetRepresentation scale];
UIImageOrientation imageOrientation = (UIImageOrientation)[assetRepresentation orientation];
dispatch_async(dispatch_get_main_queue(), ^(void) {
CGImageRef imageReference = [assetRepresentation fullResolutionImage];
/* Construct the image now */
UIImage *image = [[UIImage alloc] initWithCGImage:imageReference
scale:imageScale orientation:imageOrientation];
//Write image to doument directory
imgCount ++;
}
});
} failureBlock:^(NSError *error) {
} }];
NSLog(#"Failed to enumerate the asset groups."); }];
})
NSLog(#"Total Image Count %d",imgCount);
The code block below counts all videos and photos:
__block int videoCount = 0;
__block int photoCount = 0;
ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc]init];
[assetLibrary
enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group == nil) {
// enumeration complete
return;
}
int total = group.numberOfAssets;
[group setAssetsFilter:[ALAssetsFilter allVideos]];
int groupVideoCount = group.numberOfAssets;
videoCount += groupVideoCount;
photoCount += total - groupVideoCount;
}
failureBlock:^(NSError *error) {
// Handle error
}];

Upload ALAsset URL images to the server

I am trying to access iPhone's photo album photo images through ALAssetsLibrary and upload(send) the images to my server. I am being successful accessing the photo album and get the asset URL of each images, via the following code:
- (void)viewDidLoad
{
[super viewDidLoad];
void (^assetEnumerator)(struct ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result != NULL) {
NSLog(#"See Asset: %#", result);
[assets addObject:result];
// Here storing the asset's image URL's in NSMutable array urlStoreArr
NSURL *url = [[result defaultRepresentation] url];
[urlStoreArr addObject:url];
}
};
void (^assetGroupEnumerator)(struct ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop)
{
if(group != nil) {
[group enumerateAssetsUsingBlock:assetEnumerator];
}
[self.activity stopAnimating];
[self.activity setHidden:YES];
};
assets = [[NSMutableArray alloc] init];
library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:assetGroupEnumerator
failureBlock: ^(NSError *error) {
NSLog(#"Failure");
}];
urlStoreArr = [[NSMutableArray alloc] init];
}
-(void) UploadImagesToServer
{
for (int i=0; i<[urlStoreArr count]; i++)
{
// To get the each image URL here...
NSString *str = [urlStoreArr objectAtIndex:i];
NSLog(#"str: %#",str);
// Need to upload the images to my server..
}
}
I want to upload the images which i got from the device asset's URL to my server. Could someone please advise how can i program it for sending the images to the server using this case?
Thank you!
you could use the below SO post code for uploading images to server.
How can I upload a photo to a server with the iPhone?
upload image from iphone to the server folder

ALAssetsLibrary doesn't retrieve image URL's in physical iOS 4.1 iPod device?

I am trying to access iPhone's photo album photo images through ALAssetsLibrary. I could access all the photo albums and get the asset URL of each images on Simulator, via the following code:
- (void)viewDidLoad
{
[super viewDidLoad];
void (^assetEnumerator)(struct ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result != NULL) {
NSLog(#"See Asset: %#", result);
// assets is a NSMutableArray..
[assets addObject:result];
// Here storing the asset's image URL's in NSMutablearray urlStoreArr
NSURL *url = [[result defaultRepresentation] url];
[urlStoreArr addObject:url];
}
};
void (^assetGroupEnumerator)(struct ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop)
{
if(group != nil) {
[group enumerateAssetsUsingBlock:assetEnumerator];
}
[self.activity stopAnimating];
[self.activity setHidden:YES];
};
assets = [[NSMutableArray alloc] init];
library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:assetGroupEnumerator failureBlock: ^(NSError *error) {
NSLog(#"Failure");
}];
urlStoreArr = [[NSMutableArray alloc] init];
}
-(void) GetURLImages
{
for (int i=0; i<[urlStoreArr count]; i++)
{
// To get the each image URL here...
NSString *str = [urlStoreArr objectAtIndex:i];
NSLog(#"str: %#",str);
}
}
It works as expected on iOS 4.0 Simulator, i.e, getting all the images URL's (ex: assets-library://asset/asset.JPG?id=1000000002&ext=JPG). But it doesn't retrieve image URL's on iPod 4.1 device. What could be the problem here? Can someone please help me fix so the code could also work on iPod? I'm totally confused.
Thank you!
Try what I mentioned in my comment or the following
library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
It also depends what photos you want.
void (^assetGroupEnumerator)(struct ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop)
{
if(group != nil) {
[group enumerateAssetsUsingBlock:assetEnumerator];
}
[self.activity stopAnimating];
[self.activity setHidden:YES];
/* make */ group=nil;//otherwise it is enumerated thrice.
};

retrieve video format using AssetLibrary and display on uilabel

I would like to know how to retrieve video format using AssetLibrary and display on uilabel? i read on forum to use ALAssetPropertyRepresentations but im just lost. some help please?
I'm not sure what you mean by "format", but this code will display the video's UTI (basically, file type) and any metadata.
ALAssetsGroupEnumerationResultsBlock assetEnumerator = ^(ALAsset *asset, NSUInteger index, BOOL *stop) {
if(asset != nil) {
ALAssetRepresentation* representation = [asset defaultRepresentation];
NSLog(#"UTI = %#", [representation UTI]);
NSLog(#"Metadata = %#", [representation metadata]);
}
};
ALAssetsLibraryGroupsEnumerationResultsBlock assetGroupEnumerator = ^(ALAssetsGroup *group, BOOL *stop) {
if(group != nil) {
[group setAssetsFilter:[ALAssetsFilter allVideos]];
[group enumerateAssetsUsingBlock:assetEnumerator];
}
};
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:assetGroupEnumerator
failureBlock:^(NSError *error) {
NSLog(#"A problem occured. %#", error);
}];
[library release];
(Code modified from here.)
If you already have your asset, the code to update the label looks like this:
myLabel.text = [[myAsset defaultRepresentation] UTI];

load the video type in the photo album using AssetsLibrary in objective C

i came across the following piece of code which loads everything from photo album but what should i do if i only wanted to load the video type in the photo album on a table?
thanks in advance.
void (^assetEnumerator)(struct ALAsset *,NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop){
if(result != nil){
NSLog(#"Asset:%#", result);
[Asset addObject:result];
}
};
void (^assetGroupEnumerator)(struct ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop){
if(group != nil){
[group enumerateAssetsUsingBlock: assetEnumerator];
}
[self.tableView reloadData];
};
Asset = [[NSMutableArray alloc] init];
ALAssetsLibrary *Library = [[ALAssetsLibrary alloc] init];
[Library enumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:assetGroupEnumerator
failureBlock:^(NSError *error){
NSLog(#"error occurred");
}];
Have a look at the -valueForProperty: instance method of ALAsset. Specifically, in your example with ALAsset *result:
if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) {
// asset is a video
}
I think this will be better.
if ([result valueForProperty:ALAssetPropertyType] == ALAssetTypeVideo) {
// asset is a video
}