如何将NSUrl转换为NSString?

AVAssetExportSession完成导出video后。 我打算通过Youtube上传videopath来上传。 但[GDataUtilities MIMETypeForFileAtPath:path defaultMIMEType:@"video/mp4"]; 它只接受NSString 。 是否有可能将NSUrl转换为NSString的video文件path。

我已经尝试使用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 at

 NSString *path = [ExportoutputURL absoluteString]; 

是否有可能将NSUrl转换为NSString的video文件path。

是。 发送一个absoluteString消息。

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

如果你想要一个path,发送一个path消息的URL。 表示URL的string通常不是有效的path; 如果你想要一个path,请问一个。

至于崩溃,并不意味着absoluteString是错误的。 将absoluteStringstring发送到NSURL对象是获取表示URL的NSString对象的正确方法。 问题在别的地方。

错误EXC_BAD_ACCESS at

 NSString *path = [ExportoutputURL absoluteString]; 

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

我的猜测是,问题是这样的:

 ExportoutputURL = session.outputURL; 

将URL分配给ExportoutputURL实例variables,但不保留该对象或创build自己的副本。 因此,你不拥有这个对象,这意味着你没有保持它的活力。 它可能在任何时候死亡,很可能在这个方法( exportDidFinish:之后返回。

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

有三个简单的解决scheme:

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

无论哪种方式,你都会拥有这个对象,并且这个对象会一直存在,直到你释放它为止。 因此,当你完成它的时候,你将需要释放它(在dealloc ,如果不是更早的话),这样你就不会泄漏它。

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

 NSString *path = [[NSString alloc] initWithString:[url path]]; ? 

使用MiekNepster提到的absolutePathpath 。 扩大他们的答案,区别在于前缀。

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

只要你能这样做。

 NSString *myString = [myURL absoluteString];