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'