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

时间:2014-06-25 10:32:38

标签: ios alassetslibrary assetslibrary

自从超过一周以来,我一直面临这个问题,我无法从资产库网站获取原始照片/视频路径。

我可以通过将此文件保存到我的应用程序SandBox来获取其数据,但在这里我想避免这些事情再次创建该文件的另一个副本。

我已将此投资于DropBox,它们直接从assets-library url上传。所以,请帮我解决这个问题。

提前致谢。

这是我的代码:

-(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;
}

1 个答案:

答案 0 :(得分:2)

  

我希望这会对你有所帮助

var exitLoops = false;
for (var i = 0; i < x; i++) {
    for (var j = 0; j < y; j++) {
        for (var k = 0; k < z; k++) {
            if (something) {
                exitLoops = true;
                break;
            }
        }
        if (exitLoops) break;
    }
    if (exitLoops) break;
}