Domanda

Con il nuovo quadro raccolta risorse disponibili in iOS 4 vedo che posso ottenere l'url per un dato video utilizzando l'UIImagePickerControllerReferenceURL. L'URL restituito è nel seguente formato:

assets-library://asset/asset.M4V?id=1000000004&ext=M4V

Sto cercando di caricare il video su un sito web in modo rapido come un proof of concept che sto cercando il seguente

NSData *data = [NSData dataWithContentsOfURL:videourl];
[data writeToFile:tmpfile atomically:NO];

I dati non viene mai inizializzato in questo caso. Qualcuno è riuscito a accedere l'URL direttamente tramite la nuova libreria beni? Grazie per il vostro aiuto.

È stato utile?

Soluzione

Ecco una rapida soluzione pulita per ottenere i video come NSData. Esso utilizza il framework foto come ALAssetLibrary è deprecato come di iOS9:

IMPORTANTE

Il quadro elementi di una libreria è deprecato a partire da iOS 9.0. Invece, utilizzare le foto quadro, invece, che in iOS 8.0 e successive offre più funzionalità e prestazioni migliori per lavorare con libreria di foto di un utente. Per ulteriori informazioni, vedere foto quadro di riferimento.

import Photos

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    self.dismissViewControllerAnimated(true, completion: nil)

    if let referenceURL = info[UIImagePickerControllerReferenceURL] as? NSURL {
        let fetchResult = PHAsset.fetchAssetsWithALAssetURLs([referenceURL], options: nil)
        if let phAsset = fetchResult.firstObject as? PHAsset {
            PHImageManager.defaultManager().requestAVAssetForVideo(phAsset, options: PHVideoRequestOptions(), resultHandler: { (asset, audioMix, info) -> Void in
                if let asset = asset as? AVURLAsset {
                    let videoData = NSData(contentsOfURL: asset.URL)

                    // optionally, write the video to the temp directory
                    let videoPath = NSTemporaryDirectory() + "tmpMovie.MOV"
                    let videoURL = NSURL(fileURLWithPath: videoPath)
                    let writeResult = videoData?.writeToURL(videoURL, atomically: true)

                    if let writeResult = writeResult where writeResult {
                        print("success")
                    }
                    else {
                        print("failure")
                    }
                }
            })
        }
    }
}

Altri suggerimenti

Io uso della categoria su ALAsset:

static const NSUInteger BufferSize = 1024*1024;

@implementation ALAsset (Export)

- (BOOL) exportDataToURL: (NSURL*) fileURL error: (NSError**) error
{
    [[NSFileManager defaultManager] createFileAtPath:[fileURL path] contents:nil attributes:nil];
    NSFileHandle *handle = [NSFileHandle fileHandleForWritingToURL:fileURL error:error];
    if (!handle) {
        return NO;
    }

    ALAssetRepresentation *rep = [self defaultRepresentation];
    uint8_t *buffer = calloc(BufferSize, sizeof(*buffer));
    NSUInteger offset = 0, bytesRead = 0;

    do {
        @try {
            bytesRead = [rep getBytes:buffer fromOffset:offset length:BufferSize error:error];
            [handle writeData:[NSData dataWithBytesNoCopy:buffer length:bytesRead freeWhenDone:NO]];
            offset += bytesRead;
        } @catch (NSException *exception) {
            free(buffer);
            return NO;
        }
    } while (bytesRead > 0);

    free(buffer);
    return YES;
}

@end

Questo non è il modo migliore per farlo. Sto rispondere a questa domanda nel caso in cui un altro utente SO incontra lo stesso problema.

In fondo la mia necessità era quella di essere in grado di spooling il file video in un file tmp così posso caricarlo su un sito web utilizzando ASIHTTPFormDataRequest. C'è probabilmente un modo per lo streaming dal bene URL per il caricamento ASIHTTPFormDataRequest ma non riuscivo a capirlo. Invece ho scritto la seguente funzione per rilasciare il file in un file tmp da aggiungere alla ASIHTTPFormDataRequest.

+(NSString*) videoAssetURLToTempFile:(NSURL*)url
{

    NSString * surl = [url absoluteString];
    NSString * ext = [surl substringFromIndex:[surl rangeOfString:@"ext="].location + 4];
    NSTimeInterval ti = [[NSDate date]timeIntervalSinceReferenceDate];
    NSString * filename = [NSString stringWithFormat: @"%f.%@",ti,ext];
    NSString * tmpfile = [NSTemporaryDirectory() stringByAppendingPathComponent:filename];

    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
    {

        ALAssetRepresentation * rep = [myasset defaultRepresentation];

        NSUInteger size = [rep size];
        const int bufferSize = 8192;

        NSLog(@"Writing to %@",tmpfile);
        FILE* f = fopen([tmpfile cStringUsingEncoding:1], "wb+");
        if (f == NULL) {
            NSLog(@"Can not create tmp file.");
            return;
        }

        Byte * buffer = (Byte*)malloc(bufferSize);
        int read = 0, offset = 0, written = 0;
        NSError* err;
        if (size != 0) {
            do {
                read = [rep getBytes:buffer
                          fromOffset:offset
                              length:bufferSize 
                               error:&err];
                written = fwrite(buffer, sizeof(char), read, f);
                offset += read;
            } while (read != 0);


        }
        fclose(f);


    };


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

    };

    if(url)
    {
        ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
        [assetslibrary assetForURL:url 
                       resultBlock:resultblock
                      failureBlock:failureblock];
    }

    return tmpfile;
}

Non ci si va ...

AVAssetExportSession* m_session=nil;

-(void)export:(ALAsset*)asset withHandler:(void (^)(NSURL* url, NSError* error))handler
{
    ALAssetRepresentation* representation=asset.defaultRepresentation;
    m_session=[AVAssetExportSession exportSessionWithAsset:[AVURLAsset URLAssetWithURL:representation.url options:nil] presetName:AVAssetExportPresetPassthrough];
    m_session.outputFileType=AVFileTypeQuickTimeMovie;
    m_session.outputURL=[NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%f.mov",[NSDate timeIntervalSinceReferenceDate]]]];
    [m_session exportAsynchronouslyWithCompletionHandler:^
     {
         if (m_session.status!=AVAssetExportSessionStatusCompleted)
         {
             NSError* error=m_session.error;
             m_session=nil;
             handler(nil,error);
             return;
         }
         NSURL* url=m_session.outputURL;
         m_session=nil;
         handler(url,nil);
     }];
}

È possibile utilizzare un tasto di preset diverso se si vuole ri-codificare il filmato (AVAssetExportPresetMediumQuality per esempio)

Questa è la Obiettivo C soluzione Alonzo risposta, Usare il foto quadro

  -(NSURL*)createVideoCopyFromReferenceUrl:(NSURL*)inputUrlFromVideoPicker{

        NSURL __block *videoURL;
        PHFetchResult *phAssetFetchResult = [PHAsset fetchAssetsWithALAssetURLs:@[inputUrlFromVideoPicker ] options:nil];
        PHAsset *phAsset = [phAssetFetchResult firstObject];
        dispatch_group_t group = dispatch_group_create();
        dispatch_group_enter(group);

        [[PHImageManager defaultManager] requestAVAssetForVideo:phAsset options:nil resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {

            if ([asset isKindOfClass:[AVURLAsset class]]) {
                NSURL *url = [(AVURLAsset *)asset URL];
                NSLog(@"Final URL %@",url);
                NSData *videoData = [NSData dataWithContentsOfURL:url];

                // optionally, write the video to the temp directory
                NSString *videoPath = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%f.mp4",[NSDate timeIntervalSinceReferenceDate]]];

                videoURL = [NSURL fileURLWithPath:videoPath];
                BOOL writeResult = [videoData writeToURL:videoURL atomically:true];

                if(writeResult) {
                    NSLog(@"video success");
                }
                else {
                    NSLog(@"video failure");
                }
                 dispatch_group_leave(group);
                // use URL to get file content
            }
        }];
        dispatch_group_wait(group,  DISPATCH_TIME_FOREVER);
        return videoURL;
    }

questo dalla risposta di Zoul grazie

Similar Code in Xamarin C#

Xamarin C # Equivalente

IntPtr buffer = CFAllocator.Malloc.Allocate(representation.Size);
NSError error;
            nuint buffered = representation.GetBytes(buffer, Convert.ToInt64(0.0),Convert.ToUInt32(representation.Size),out error);

            NSData sourceData = NSData.FromBytesNoCopy(buffer,buffered,true);
            NSFileManager fileManager = NSFileManager.DefaultManager;
            NSFileAttributes attr = NSFileAttributes.FromDictionary(NSDictionary.FromFile(outputPath));
            fileManager.CreateFile(outputPath, sourceData,attr);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top