【问题标题】:How to convert NSUrl to NSString?如何将 NSUrl 转换为 NSString?
【发布时间】:2012-02-10 15:48:53
【问题描述】:

AVAssetExportSession 完成导出视频后。 我计划通过 Youtube 上传视频路径。 但是[GDataUtilities MIMETypeForFileAtPath:path defaultMIMEType:@"video/mp4"]; 它只接受NSString。 是否可以将 NSUrl 转换为 NSString 作为视频文件路径。

我尝试使用NSString *path = [ExportoutputURL absoluteString]; 但它崩溃了。

这是代码

- (void)exportDidFinish:(AVAssetExportSession*)session {
    ExportoutputURL = session.outputURL;

    _exporting = NO;
    NSIndexPath *exportCellIndexPath = [NSIndexPath indexPathForRow:2 inSection:kProjectSection];
    ExportCell *cell = (ExportCell*)[self.tableView cellForRowAtIndexPath:exportCellIndexPath];
    cell.progressView.progress = 1.0;
    [cell setProgressViewHidden:YES animated:YES];
    [self updateCell:cell forRowAtIndexPath:exportCellIndexPath];

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:ExportoutputURL]) {
        [library writeVideoAtPathToSavedPhotosAlbum:ExportoutputURL
                                    completionBlock:^(NSURL *assetURL, NSError *error){
                                        dispatch_async(dispatch_get_main_queue(), ^{
                                            if (error) {
                                                NSLog(@"writeVideoToAssestsLibrary failed: %@", error);
                                                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[error localizedDescription]
                                                                                                    message:[error localizedRecoverySuggestion]
                                                                                                   delegate:nil
                                                                                          cancelButtonTitle:@"OK"
                                                                                          otherButtonTitles:nil];
                                                [alertView show];
                                                [alertView release];
                                            }
                                            else {
                                                _showSavedVideoToAssestsLibrary = YES;
                                                ExportCell *cell = (ExportCell*)[self.tableView cellForRowAtIndexPath:exportCellIndexPath];
                                                [cell setDetailTextLabelHidden:NO animated:YES];
                                                [self updateCell:cell forRowAtIndexPath:exportCellIndexPath];
                                                NSArray *modes = [[[NSArray alloc] initWithObjects:NSDefaultRunLoopMode, UITrackingRunLoopMode, nil] autorelease];
                                                [self performSelector:@selector(hideCameraRollText) withObject:nil afterDelay:5.0 inModes:modes];
                                            }
                                        });

                                    }];
    }
    [library release];
}

- (void)uploadVideoFile {

    NSString *devKey = DEVELOPER_KEY;

    GDataServiceGoogleYouTube *service = [self youTubeService];
    [service setYouTubeDeveloperKey:devKey];

    NSURL *url = [GDataServiceGoogleYouTube youTubeUploadURLForUserID:kGDataServiceDefaultUser];

    // load the file data
    NSString *path = [ExportoutputURL absoluteString];//[[NSBundle mainBundle] pathForResource:@"video_2451" ofType:@"mp4"];//[mFilePathField stringValue];
    NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:path];
    NSString *filename = [path lastPathComponent];

    // gather all the metadata needed for the mediaGroup
    NSString *titleStr = @"Upload Test";//[mTitleField stringValue];
    GDataMediaTitle *title = [GDataMediaTitle textConstructWithString:titleStr];

    NSString *categoryStr = @"Entertainment";//[[mCategoryPopup selectedItem] representedObject];
    GDataMediaCategory *category = [GDataMediaCategory mediaCategoryWithString:categoryStr];
    [category setScheme:kGDataSchemeYouTubeCategory];

    NSString *descStr = @"GData Description";//[mDescriptionField stringValue];
    GDataMediaDescription *desc = [GDataMediaDescription textConstructWithString:descStr];

    NSString *keywordsStr = @"RAGOpoR Demo";//[mKeywordsField stringValue];
    GDataMediaKeywords *keywords = [GDataMediaKeywords keywordsWithString:keywordsStr];

    BOOL isPrivate = NO;//([mPrivateCheckbox state] == NSOnState);

    GDataYouTubeMediaGroup *mediaGroup = [GDataYouTubeMediaGroup mediaGroup];
    [mediaGroup setMediaTitle:title];
    [mediaGroup setMediaDescription:desc];
    [mediaGroup addMediaCategory:category];
    [mediaGroup setMediaKeywords:keywords];
    [mediaGroup setIsPrivate:isPrivate];

    NSString *mimeType = [GDataUtilities MIMETypeForFileAtPath:path
                                               defaultMIMEType:@"video/mp4"];

    // create the upload entry with the mediaGroup and the file
    GDataEntryYouTubeUpload *entry;
    entry = [GDataEntryYouTubeUpload uploadEntryWithMediaGroup:mediaGroup
                                                    fileHandle:fileHandle
                                                      MIMEType:mimeType
                                                          slug:filename];

    SEL progressSel = @selector(ticket:hasDeliveredByteCount:ofTotalByteCount:);
    [service setServiceUploadProgressSelector:progressSel];

    GDataServiceTicket *ticket;
    ticket = [service fetchEntryByInsertingEntry:entry
                                      forFeedURL:url
                                        delegate:self
                               didFinishSelector:@selector(uploadTicket:finishedWithEntry:error:)];
    [self setUploadTicket:ticket];
    GTMHTTPUploadFetcher *uploadFetcher = (GTMHTTPUploadFetcher *)[ticket objectFetcher];

}

错误 EXC_BAD_ACCESS 在

NSString *path = [ExportoutputURL absoluteString];

【问题讨论】:

  • 这应该可以,也许你之前发布了 NSURL?你能显示一些代码吗?
  • 你正在做的应该工作。请发布更多代码。
  • 你的 NSURL 实例不是 nil 吗?
  • 检查 ExportoutputURL 是否为 nil。如果它是你可能发布的某个地方之前。启动后立即添加[ExportoutputURL retain];,看看它是否仍然崩溃。如果不是,那就是你的问题,你应该再深入一点。
  • 试试relativePath而不是absoluteString

标签: objective-c ios cocoa-touch cocoa


【解决方案1】:

是否可以将 NSUrl 转换为 NSString 用于视频文件路径。

是的。给它发送absoluteString 消息。

我尝试使用 NSString *path = [ExportoutputURL absoluteString];但它崩溃了。

如果您需要路径,请向 URL 发送 path 消息。表示 URL 的字符串通常不是有效路径;如果您想要一条路径,请向它索取。

至于崩溃,并不代表absoluteString错了。将absoluteString 发送到 NSURL 对象是获取表示 URL 的 NSString 对象的正确方法。问题出在其他地方。

错误 EXC_BAD_ACCESS 在

NSString *path = [ExportoutputURL absoluteString];

这可能意味着ExportoutputURL 指向的东西不是nil,但也不是有效对象。它可能在某个时候指向了一个 NSURL 对象,但现在没有。

我的猜测是问题出在:

ExportoutputURL = session.outputURL;

您将 URL 分配给 ExportoutputURL 实例变量,但您不保留对象或制作自己的副本。因此,你不拥有这个对象,这意味着你没有让它活着。它随时可能死掉,很可能是在这个方法 (exportDidFinish:) 返回之后。

崩溃是因为您稍后在 URL 对象已经死亡之后调用uploadVideoFile。您仍然有一个指向它的指针,但该对象不再存在,因此向它发送消息(任何消息)会导致崩溃。

有三个简单的解决方案:

  1. 在将 URL 对象分配给实例变量时保留它。
  2. 制作您自己的 URL 对象副本并将其分配给实例变量。
  3. 使用strong 关键字或copy 关键字将ExportoutputURL 声明为属性,并将对象分配给属性,而不是实例变量。这将调用属性的 setter,如果您合成或正确实现它,它将为您保留或复制 URL。

无论哪种方式,您都将拥有该对象,这将使其保持活动状态,直到您释放它。因此,您需要在完成后释放它(在dealloc,如果不是更早的话),这样您就不会泄露它。

这一切都假设您没有使用 ARC。如果您使用的是 Xcode 4.2 或更高版本,并且可能需要 iOS 4 或更高版本,您应该将您的项目迁移到 ARC,因为它使很多事情变得更加简单。如果您使用 ARC,则不需要保留或复制此对象,这意味着现在迁移到 ARC 是第四种解决方案(但肯定是更大规模的解决方案)。

【讨论】:

    【解决方案2】:

    MiekNepster 所述,使用 absolutePathpath。扩展他们的答案,两者之间的区别在于前缀。

    NSString* string1 = [url absoluteString]; // @"file:///Users/jackbrown/Music/song name.mp3"
    NSString* string2 = [url path]; // @"/Users/jackbrown/Music/song name.mp3"`
    

    【讨论】:

      【解决方案3】:
      NSString *path = [[NSString alloc] initWithString:[url path]];  ?
      

      【讨论】:

        【解决方案4】:

        使用这个。我想它会对你有所帮助。

        在目标c中

        NSString *testString = testUrl.absoluteString;
        

        在斯威夫特中

        var testString : String = testUrl.absoluteString
        

        【讨论】:

          【解决方案5】:

          你可以这样做。

          NSString *myString = [myURL absoluteString];
          

          【讨论】:

            猜你喜欢
            • 2011-12-26
            • 2014-10-25
            • 1970-01-01
            • 2012-04-05
            • 2016-04-07
            • 1970-01-01
            • 1970-01-01
            • 2016-07-28
            • 1970-01-01
            相关资源
            最近更新 更多