Upload ALAsset URL images to the server - iphone

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

Related

Read images from a iphone album using ALAssetsLibrary

I am trying to write a method that read assets from a album I am getting all images from all album using ALAsset, my question is how can I customize this method to read images from myAlbum
/**
* This method is used to get all images from myAlbum
* folder in device if getAlbumImages is set to 1
* else loads all images from devices library
*/
-(void)readImages:(int)getAlbumImages
{
imageArray = [[NSArray alloc] init];
mutableArray =[[NSMutableArray alloc]init];
NSMutableArray* assetURLDictionaries = [[NSMutableArray alloc] init];
library = [[ALAssetsLibrary alloc] init];
if(getFolderImages == 1) {
// Load images from Shareaflash folder
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *documentdataPath = [documentsDirectory stringByAppendingPathComponent:#"myFolder"];
NSLog(#"documentdataPath %#",documentdataPath);
} else {
// Load all images
}
void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result != nil) {
if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
[assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]];
NSURL *url = (NSURL*) [[result defaultRepresentation]url];
[library assetForURL:url resultBlock:^(ALAsset *asset) {
[mutableArray addObject:asset];
if ([mutableArray count]==count)
{
imageArray =[[NSArray alloc] initWithArray:mutableArray];
[self allPhotosCollected:imageArray];
}
}
failureBlock:^(NSError *error){ NSLog(#"operation was not successfull!"); } ];
}
}
};
NSMutableArray *assetGroups = [[NSMutableArray alloc] init];
void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop) {
if(group != nil) {
[group enumerateAssetsUsingBlock:assetEnumerator];
[assetGroups addObject:group];
count=[group numberOfAssets];
}
};
assetGroups = [[NSMutableArray alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:assetGroupEnumerator
failureBlock:^(NSError *error) { NSLog(#"There is an error"); }];
}
How can I use documentdataPath to load assets
When you call enumerateGroupsWithTypes, in the block, check the asset group name (valueForProperty:). Then, instead of adding all of the asset groups to your assetGroups list you can only add the appropriate ones.

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

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];

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
}