创建快速关闭并将其作为完成块传递给Objective-C类 [英] Create swift closure and pass it to objective-c class as completion block
问题描述
现在,我正在基于Objective-C的iOS应用程序中添加Swift代码。
但是从Swift端调用Objective-C方法时遇到问题。 / p>
我想在 View $ c中的
UIImageView
上绘制图像$ c>或 ViewController
用Swift编写。
另一方面,我的照片库包装器类是用Objective-C编写的。
我的照片库包装器类如下。
・ MyPhotoLibraryMgr.h
typedef void(^ AppPhotoLibraryGetImageCompletion)(UIImage * image,NSError *错误);
-(void)getImageWithTarget:(NSInteger)index
targetSize:(CGSize)targetSize
完成:(AppPhotoLibraryGetImageCompletion)完成;
・ MyPhotoLibraryMgr.m
-(void)getImageWithTarget:(NSInteger)index
targetSize:(CGSize)targetSize
完成时间:
(AppPhotoLibraryGetImageCompletion)完成
{
PHAsset * asset = _myAsetsList [index];
PHImageRequestOptions * options = [[PHImageRequestOptions alloc] init];
options.resizeMode = PHImageRequestOptionsResizeModeFast;
options.deliveryMode = PHImageRequestOptionsDeliveryModeOpportunistic;
[[PHImageManager defaultManager] requestImageForAsset:asset
targetSize:targetSize
contentMode:PHImageContentModeAspectFill
options:options
resultHandler:^(UIImage * result,NSDictionary * info){
NSError * error = [info objectForKey:PHImageErrorKey];
if(error){
NSLog(@ error =%@,error);
}
完成(结果,错误);
}];
}
我可以在 View中获取并绘制图像由目标C编写的code>或
ViewController
,如下所示,
・ MyViewController.m
AppPhotoLibraryMgr * photoLibMgr = [AppPhotoLibraryMgr sharedInstance];
AppPhotoLibraryGetImageCompletion完成= ^(UIImage * image,NSError * error){
imageView.image = image;
};
[[AppPhotoLibraryMgr sharedInstance] getImageWithTarget:index
targetSize:imageView.frame.size
complete:completion];
但是我无法在 View
或用Swift编写的 ViewController
。
・ MySwiftViewController.swift
让完成完成= {{image:UIImage,error:NSError)->
中的AppPhotoLibraryGetImageCompletion让imageView = UIImageView(image:image)
}
photoLibMgr?.getImageWithTarget(index,targetSize:size,complete:completion)
来自Xcode的错误消息是
无法将类型'UIImage,NSError->
AppPhotoLibraryGetImageCompletion的值转换为预期的参数类型
'AppPhotoLibraryGetImageCompletion'
您的 completion
变量声明在 MySwiftViewController.swift中是错误的文件。
像这样编辑它:
让完成:AppPhotoLibraryGetImageCompletion = {(图像,错误)在
中,让imageView = UIImageView(image:image)
}
OR
如果您不想将完成处理程序声明为变量,则只需执行以下操作:
李nphoneLoginController.shared()。getImageWithTarget(index,targetSize:size){(图像,错误)在
中,让imageView = UIImageView(image:image)
}
Now I'm adding Swift code on objective-C based iOS application.
But I have a problem in calling the objective-C method from Swift side.
I'd like to get and draw image on UIImageView
in View
or ViewController
which is written with Swift.
On the other hand my wrapper class of Photos Library is written with objective-C.
My wrapper class of Photos Library is below.
・MyPhotoLibraryMgr.h
typedef void (^AppPhotoLibraryGetImageCompletion)(UIImage *image, NSError *error);
- (void)getImageWithTarget:(NSInteger)index
targetSize:(CGSize)targetSize
completion:(AppPhotoLibraryGetImageCompletion)completion;
・MyPhotoLibraryMgr.m
- (void)getImageWithTarget:(NSInteger)index
targetSize:(CGSize)targetSize
completion:
(AppPhotoLibraryGetImageCompletion)completion
{
PHAsset *asset = _myAsetsList[index];
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.resizeMode = PHImageRequestOptionsResizeModeFast;
options.deliveryMode = PHImageRequestOptionsDeliveryModeOpportunistic;
[[PHImageManager defaultManager] requestImageForAsset:asset
targetSize:targetSize
contentMode:PHImageContentModeAspectFill
options:options
resultHandler:^(UIImage *result, NSDictionary *info) {
NSError *error = [info objectForKey:PHImageErrorKey];
if (error) {
NSLog(@"error=%@", error);
}
completion(result, error);
}];
}
I can get and draw image in View
or ViewController
written by objective-C like below,
・MyViewController.m
AppPhotoLibraryMgr *photoLibMgr = [AppPhotoLibraryMgr sharedInstance];
AppPhotoLibraryGetImageCompletion completion = ^(UIImage *image, NSError *error) {
imageView.image = image;
};
[[AppPhotoLibraryMgr sharedInstance] getImageWithTarget:index
targetSize:imageView.frame.size
completion:completion];
But I can't get image in View
or ViewController
written in Swift.
・MySwiftViewController.swift
let completion = {(image: UIImage, error: NSError) -> AppPhotoLibraryGetImageCompletion in
let imageView = UIImageView(image: image)
}
photoLibMgr?.getImageWithTarget(index, targetSize: size, completion: completion)
Error message from Xcode is
Cannot convert value of type 'UIImage, NSError -> AppPhotoLibraryGetImageCompletion" to expected argument type 'AppPhotoLibraryGetImageCompletion'
Your declaration of completion
variable is wrong in MySwiftViewController.swift file.
Edit it like this:
let completion: AppPhotoLibraryGetImageCompletion = { (image, error) in
let imageView = UIImageView(image: image)
}
OR
If you don't want to declare completion handler as variable, then simply do the:
LinphoneLoginController.shared().getImageWithTarget(index, targetSize: size) { (image, error) in
let imageView = UIImageView(image: image)
}
这篇关于创建快速关闭并将其作为完成块传递给Objective-C类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!