如何从.m函数到.mm类Objective-C与Qt混合的结果

问题描述

我有 Qt 应用程序,我正在调用 UIImagePickerController 来获取用户选择的电影的文件路径。所以我在 Objective-C 中有静态函数,它们从 .m 文件调用函数,当用户选择电影时,我可以在另一个 .m 函数中读取选定的路径。问题是,如何将此 filePath 放入某个 Objective-C 类中,我可以在其中调用一个 Qt 类(如 Singleton)并将此结果传递给适当的类? Objective-C CL_image_call.mm

#include "cl_image_call.h"
#include "cl_image_func.h"

myclimageClass* CL_image_obj=NULL;

int CL_ImageCCall::CL_objectiveC_Call() {
  //Objective C code calling..... 
    if( CL_image_obj==NULL ) {
      //Allocating the new object for the objective C class we created
        CL_image_obj=[[myclimageClass alloc]init]; 
    }
  return 1;
}
void CL_ImageCCall::CL_openMedia() {
    [CL_image_obj openMedia];
}

CL_image_call.h

#include <QImage>
#include <QString>
#include <QDebug>
#include <stdio.h>
#include "my_singleton.h"

class CL_ImageCCall
{
  public:
    static int CL_objectiveC_Call(); 
    static void CL_openMedia();  
};

CL_image_func.h

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <CoreGraphics/CoreGraphics.h>
#import <UIKit/UIKit.h>
#import <MobileCoreServices/MobileCoreServices.h>

@interface myclimageClass:NSObject
-(void)openMedia;
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediawithInfo:(NSDictionary *)info;
@end

CL_image_func.m

#import "cl_image_func.h"

@interface QIOSViewController : UIViewController
@end

@implementation myclimageClass
UIImagePickerController *picker=NULL;
UIViewController *viewController=NULL;
Nsstring *f_path;

-(void)openMedia {
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    
    viewController = (UIViewController *)([keyWindow rootViewController]);
        if (!viewController)
            return;
    
    if([UIImagePickerController isSourceTypeAvailable:
                            UIImagePickerControllerSourceTypePhotoLibrary]) {

         picker= [[UIImagePickerController alloc] init];
         picker.delegate = self;
         picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
         picker.mediaTypes = [NSArray arrayWithObjects:(Nsstring *)kUTTypeMovie,nil];

         [viewController presentViewController:picker animated:YES completion:NULL];
    }
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediawithInfo:(NSDictionary *)info {
    Nsstring *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0];
    Nsstring *filePath =  [docDirPath stringByAppendingPathComponent:@"movie.mov"];
    NSLog (@"File Path = %@",filePath);
    //filePath is what I need to pass to the rest of my app
    
    [viewController dismissViewControllerAnimated:YES completion:nil];
}

所以在 CL_image_call.h 中,我有我的“my_singleton”,我可以在其中访问我的整个应用程序,但是如何获取该类的文件路径?

最好的问候

马雷克

解决方法

这是导出媒体的代码:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0];
    NSString *filePath =  [docDirPath stringByAppendingPathComponent:@"movie.mov"];
    NSLog (@"File Path = %@",filePath);
    //filePath is what I need to pass to the rest of my app
    NSURL *inputURL  = [info objectForKey:UIImagePickerControllerMediaURL];
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:inputURL options:nil];
    // config the session,maybe some option is not what you need,just config by what you need
    AVAssetExportSession *session = [[AVAssetExportSession alloc] initWithAsset:asset
                                                                     presetName:AVAssetExportPresetMediumQuality];
    session.outputURL = [NSURL fileURLWithPath:filePath];
    session.outputFileType = AVFileTypeMPEG4;
    session.shouldOptimizeForNetworkUse = YES;
    [session exportAsynchronouslyWithCompletionHandler:^(void)
     {
        // do a completion handler
    }];
    
    [viewController dismissViewControllerAnimated:YES completion:nil];
}

然后可以通过filePath访问输出,需要的时候使用。

,

我已经根据您的建议修改了代码,并且它做了它应该做的。我在我的 Qt 类中设置了 moviePath,然后我用这个路径调用了 Objective-C 代码

NSString *f_path;
  //Your objective c code here....

-(void)openMedia:(NSString*)moviePath {
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    
    f_path=moviePath.copy;
    viewController = (UIViewController *)([keyWindow rootViewController]);
        if (!viewController)
            return;
    if([UIImagePickerController isSourceTypeAvailable:
                            UIImagePickerControllerSourceTypePhotoLibrary]) {

         picker= [[UIImagePickerController alloc] init];
         picker.delegate = self;
         picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
         picker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeMovie,nil];

         [viewController presentViewController:picker animated:YES completion:NULL];
        
    }
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSURL *inputURL  = [info objectForKey:UIImagePickerControllerMediaURL];
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:inputURL options:nil];
    // config the session,just config by what you need
    AVAssetExportSession *session = [[AVAssetExportSession alloc] initWithAsset:asset
                                                                         presetName:AVAssetExportPresetMediumQuality];
    session.outputURL = [NSURL fileURLWithPath:f_path];
    session.outputFileType = AVFileTypeMPEG4;
    session.shouldOptimizeForNetworkUse = YES;
    [session exportAsynchronouslyWithCompletionHandler:^(void)
    {
        // do a completion handler
        NSLog (@"export complete = %@",f_path);
    }];
    
    [viewController dismissViewControllerAnimated:YES completion:nil];
}

通过一些技巧,我可以在 Qt 应用程序中观看这部电影。缺少的一件事: 我需要以某种方式知道会话已完成,以刷新 MediaPlayer 的文件路径。我如何通知我的基类 CL_ImageCCall 。或者实际上也许是另一个 Objective-C++ 类,我可以在其中混合 Qt 和 Objective-C?

最好的问候并感谢您的帮助

马雷克

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...