简体   繁体   English

如何从iOS / iPhone中的资产库获取照片/视频源路径?

[英]How to get photo/video origin path from assets-library in iOS/iPhone?

I have been faced this issue since more than a week, i am not able to get original photo/video path from assets-library url. 我已经遇到这个问题,因为一个多星期以来,我无法从资产库网站获取原始照片/视频路径。

I can get its data by saving this file to my app SandBox, but here i want to avoid this things to again create another copy of that file. 我可以通过将此文件保存到我的应用程序SandBox来获取其数据,但在这里我想避免这些事情再次创建该文件的另一个副本。

I have investing this in DropBox, they are directly uploading from assets-library url. 我在DropBox中投资,他们直接从assets-library url上传。 So, please help me to get out this issue. 所以,请帮我解决这个问题。

Thanks in advance. 提前致谢。

Here is my code: 这是我的代码:

-(NSString*) videoAssetURLToTempFile:(NSString*)combineURL{
self.activityIndicator.hidden = FALSE;
NSLog(@"combineURL: %@",combineURL);
NSArray *arr = [combineURL componentsSeparatedByString:@"->"];
NSString *index = [arr objectAtIndex:0];

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",[arr objectAtIndex:1]]];

//url like: "assets-library://asset/asset.MOV?id=78988A2B-203B-41B9-8EDA-F3029303DFBF&ext=MOV"

NSString * surl = [url absoluteString];
NSString * ext = [surl substringFromIndex:[surl rangeOfString:@"ext="].location + 4];

NSTimeInterval ti = [[NSDate date]timeIntervalSinceReferenceDate];
NSString *fname = [NSString stringWithFormat: @"%f.%@",ti,ext];

NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"SAVED_PHOTOS"];
// New Folder is your folder name
NSError *error = nil;
if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath])
    [[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:&error];

NSString *tmpfile = [stringPath stringByAppendingPathComponent:fname];

ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset){
    ALAssetRepresentation * rep = [myasset defaultRepresentation];
    NSLog(@"video url: %@",rep.url);
    NSUInteger size = [rep size];

    NSLog(@"file size: %@",[NSString stringWithFormat:@"%llu",(unsigned long long)rep.size]);
    if (!appDelegate.arrFileSize) {
        appDelegate.arrFileSize = [[NSMutableArray alloc] init];
    }
    [appDelegate.arrFileSize replaceObjectAtIndex:[index integerValue] withObject:[NSString stringWithFormat:@"%llu",(unsigned long long)rep.size]];
    [appDelegate.accessToken setObject:appDelegate.arrFileSize forKey:@"FileSize"];
    [appDelegate.accessToken synchronize];
    NSLog(@"video fileSize: %@",[self convertbyteToKB_MB:(unsigned long long)rep.size]);
    unsigned long long freeSpaceSize = [self getFreeDiskspace];
    NSLog(@"freeSpaceSize: %llu",freeSpaceSize);

    if (freeSpaceSize<rep.size && rep.size<1073741824) {
        NSString *alertMsg;
        if (IS_IPHONE) {
            alertMsg = [NSString stringWithFormat:@"App requires %@ free storage space to upload this video. To proceed with this upload, please go to your iPhone's Settings and clear some space.",[self convertbyteToKB_MB:(unsigned long long)rep.size]];
        }
        else{
            alertMsg = [NSString stringWithFormat:@"App requires %@ free storage space to upload this video. To proceed with this upload, please go to your iPad's Settings and clear some space.",[self convertbyteToKB_MB:(unsigned long long)rep.size]];
        }
        dispatch_async(dispatch_get_main_queue(), ^{

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:alertMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
            dispatch_async(dispatch_get_main_queue(), ^{
                self.waitToUploadLbl.hidden = TRUE;
            });
        });

    }
    else{
        if (rep.size>=1073741824) {
            dispatch_async(dispatch_get_main_queue(), ^{
                self.waitToUploadLbl.hidden = TRUE;
            });
        }
        else{
            convertingTotal++;
            dispatch_async(dispatch_get_main_queue(), ^{
                self.waitToUploadLbl.hidden = FALSE;
            });
            const int bufferSize = 8*1024*1024;
            FILE* f = fopen([tmpfile cStringUsingEncoding:1], "wb+");
            if (f == NULL) {
                return;
            }

            Byte * buffer = (Byte*)malloc(bufferSize);
            unsigned long long read = 0, offset = 0, written = 0;
            NSError* err;
            if (size != 0) {

                do {
                    read = (unsigned long long)[rep getBytes:buffer
                                                  fromOffset:offset
                                                      length:bufferSize
                                                       error:&err];
                    written = (unsigned long long)fwrite(buffer, sizeof(char), read, f);
                    offset += read;
                } while (read != 0);
                blockedNo++;
            }
            fclose(f);
            NSLog(@"file saved to temp");
            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"total: %d blockedNo: %d",convertingTotal,blockedNo);
                self.waitToUploadLbl.hidden = TRUE;
           });
        }
    }

};

ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
{
    //NSLog(@"Can not get asset - %@",[myerror localizedDescription]);

};

if(url)
{
    ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
    [assetslibrary assetForURL:url
                   resultBlock:resultblock
                  failureBlock:failureblock];
}
self.activityIndicator.hidden = TRUE;
@try {
    [appDelegate.uploadArray1 replaceObjectAtIndex:[index integerValue] withObject:tmpfile];
    [appDelegate.accessToken setObject:appDelegate.uploadArray1 forKey:@"UploadArray"];
    [appDelegate.accessToken synchronize];
    NSLog(@"temporary path: %@",tmpfile);
    [self performSelector:@selector(uploadphotoToServer:) withObject:index afterDelay:1];
}
@catch (NSException *exception) {
    NSLog(@"Error: %@",exception);
}
@finally {
    dispatch_async(dispatch_get_main_queue(), ^{
        if (convertingTotal==blockedNo) {
            self.waitToUploadLbl.hidden = TRUE;
        }

    });
}
return tmpfile;
}

i hope this will help you 我希望这能帮到您

NSString *str_url=[arrURL objectAtIndex:indexPath.row];//put your index

                    ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];

                    NSURL* aURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@",str_url]];

                    [assetLibrary assetForURL:aURL resultBlock:^(ALAsset *asset) {

                        ALAssetRepresentation *rep = [asset defaultRepresentation];
                        Byte *buffer = (Byte*)malloc(rep.size);
                        NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
                        NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];

                        cell.imageCell.image=[UIImage imageWithData:data];


                    } failureBlock:^(NSError *error)
                     {
                         NSLog(@"Error: %@",[error localizedDescription]);
                     }];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM