6

I am trying use an Objective-C library (MWPhotoBrowser) in my Swift app. My Swift class conforms to MWPhotoBrowserDelegate protocol by implementing required methods. However, I keep getting the following error:

"Type 'PhotoLibrary' does not conform to protocol 'MWPhotoBrowserDelegate'"

Cocoa protocols seem to work fine. Has anyone encountered this issue before?

Here is the sample code:

class PhotoLibrary: UIImageView, MWPhotoBrowserDelegate {

    init() {
        super.init(frame: CGRectZero)
    }

    func numberOfPhotosInPhotoBrowser(photoBrowser: MWPhotoBrowser!) -> Int {
        return 0
    }

    func photoBrowser(photoBrowser: MWPhotoBrowser!, photoAtIndex index: Int) -> MWPhoto! {
        return nil
    }
}

Protocol definition is as follows:

@protocol MWPhotoBrowserDelegate <NSObject>

- (NSInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser;
- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSInteger)index;

@optional

- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser thumbPhotoAtIndex:(NSUInteger)index;
- (MWCaptionView *)photoBrowser:(MWPhotoBrowser *)photoBrowser captionViewForPhotoAtIndex:(NSUInteger)index;
- (NSString *)photoBrowser:(MWPhotoBrowser *)photoBrowser titleForPhotoAtIndex:(NSUInteger)index;
- (void)photoBrowser:(MWPhotoBrowser *)photoBrowser didDisplayPhotoAtIndex:(NSUInteger)index;
- (void)photoBrowser:(MWPhotoBrowser *)photoBrowser actionButtonPressedForPhotoAtIndex:(NSUInteger)index;
- (BOOL)photoBrowser:(MWPhotoBrowser *)photoBrowser isPhotoSelectedAtIndex:(NSUInteger)index;
- (void)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index selectedChanged:(BOOL)selected;
- (void)photoBrowserDidFinishModalPresentation:(MWPhotoBrowser *)photoBrowser;

@end
4

4 回答 4

4

我让 MWPhotoBrowser 使用以下代码在我的 Swift 应用程序中工作,而无需更改 MWPhotoBrowser 代码库。

func showFullScreenImage(image:UIImage) {
    let photo:MWPhoto = MWPhoto(image:image)

    self.photos = [photo]

    let browser:MWPhotoBrowser = MWPhotoBrowser(delegate: self)

    browser.displayActionButton = true
    browser.displayNavArrows = false
    browser.displaySelectionButtons = false
    browser.zoomPhotosToFill = true
    browser.alwaysShowControls = false
    browser.enableGrid = false
    browser.startOnGrid = false
    browser.enableSwipeToDismiss = true

    browser.setCurrentPhotoIndex(0)

    self.navigationController?.pushViewController(browser, animated: true)
}

func numberOfPhotosInPhotoBrowser(photoBrowser: MWPhotoBrowser!) -> UInt {
    return UInt(self.photos.count)
}

func photoBrowser(photoBrowser: MWPhotoBrowser!, photoAtIndex index: UInt) -> MWPhotoProtocol! {
    if Int(index) < self.photos.count {
        return photos.objectAtIndex(Int(index)) as! MWPhoto
    }
    return nil
}

func photoBrowserDidFinishModalPresentation(photoBrowser:MWPhotoBrowser) {
    self.dismissViewControllerAnimated(true, completion:nil)
}

我将照片设置为类变量,例如private var photos = [],并在我的应用程序的 Bridging-Header.h 文件中添加了以下导入:

#import <MWPhotoBrowser/MWPhoto.h>
#import <MWPhotoBrowser/MWPhotoBrowser.h>

我从https://github.com/mwaterfall/MWPhotoBrowser/issues/325#issuecomment-64781477获得了上述大部分代码。

于 2015-04-22T17:02:14.690 回答
2

这是我发现的:

Swift 存在 id 数据类型的问题。我不得不修改 MWPhotoBrowser 代码以将所有 id 实例替换为 (MWPhoto *)。我认为 MWPhoto * 应该首先使用。

这解决了不合格的问题。

于 2014-07-30T04:56:53.710 回答
0

xCode 7.1 - 遇到了一些问题,但让它工作。

  1. 通过可可豆荚进口。

  2. 桥接头添加:

    #import <MWPhotoBrowser/MWPhoto.h>

    #import <MWPhotoBrowser/MWPhotoBrowser.h>

  3. Build Settings -> Header Search Paths 添加“Pods”设置为递归(修复桥接头错误)

  4. Build Phases -> Link Binary with Libraries 添加:libMWPhotoBroswer、MediaPlayer.framework、libSDWebImage、libMBProgressHUD 和 libDACIrcularProgress(修复了“未找到架构 x86_64 的符号”错误)

  5. 构建设置 -> 其他链接器标志添加“-ObjC”(修复 SDWebImage 框架的错误)

希望对某人有所帮助。

于 2015-12-03T20:14:55.767 回答
0

我让它与 swift 3.0 一起工作,我按照以下步骤操作:

(1) 编辑MWPhotoBrowser.h文件并将“”查找/替换id <MWPhoto>为“ MWPhoto *

(2) 实现所需功能为:

public func numberOfPhotos(in photoBrowser: MWPhotoBrowser!) -> UInt {
    return UInt(arrPhotos.count)
}

public func photoBrowser(_ photoBrowser: MWPhotoBrowser!, photoAt index: UInt) -> MWPhoto! {
    return arrPhotos[Int(index)] as MWPhoto
}
于 2017-01-01T04:12:09.673 回答