【问题标题】:Get image URL from MWPhoto?从 MWPhoto 获取图像 URL?
【发布时间】:2011-08-22 19:55:40
【问题描述】:

我正在使用 MWPhotoBrowser (here),我正在尝试获取您当前使用的图像。

我试试这个:

MWPhoto *theObject = [photos objectAtIndex:currentPageIndex];
NSString *test = [theObject image];
NSLog(@"maybe? %@", test);

我明白了

maybe? <UIImage: 0x4e5e890>

这很好,但我想获取当前图像的 URL。

感谢您的帮助,
库尔顿

【问题讨论】:

    标签: objective-c cocoa-touch xcode ios4


    【解决方案1】:

    在 MWPhoto.h 中添加:

    - (NSURL *)url;
    - (NSString *)path;
    

    在第 45 行

    在 MWPhoto.m 中添加:

    // Return URL
    - (NSURL *)url { return self.photoURL; }
    
    // Return Path
    - (NSString *)path { return self.photoPath; }
    

    在第 88 行

    你的代码在哪里,做类似的事情

    MWPhoto *theObject = [photos objectAtIndex:currentPageIndex];
    NSString *path = [theObject path];
    NSURL *url = [theObject url];
    NSString *urlString = [url absoluteString];
    NSLog(@"path %@", path);
    NSLog(@"url %@", path);
    

    要使用 NSURL,请查看http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html

    【讨论】:

    • -[MWPhoto photoURL]: unrecognized selector sent to instance 0x4e5a8e0 -- 我认为它没有在 MWPhotoBrowser.m 文件中声明(这是我的代码所在的位置)。
    • 如果你看一下MWPhoto.h,你可以看到MWPhoto的成员是:photoPath、photoUrl、photoImage和workingInBackground。 'image' 会返回 'photoImage',但没有你想要的 photoPath 和 PhotoUrl 函数。您可以在 MWPhoto.m 中查看“图像”函数并为要返回的属性编写访问器。
    • @John:你能写一个例子吗?我可以从另一个类文件中读取什么?喜欢MWPhotoBrowser *MWBrowser = [[MWPhotoBrowser alloc] init]; NSURL *test = [MWBrowser.theObject photoURL]; NSLog(@"maybe? %@", test); 谢谢!
    • 编辑了答案,为您提供更多信息以及在哪里做事,只是被警告,没有检查这些值,您需要进行任何错误检查等,或者只是真实的小心点:)
    • 当像这样放入我的.h ` #pragma mark NSObject // Return URL - (NSURL *)url { return self.photoURL; } // 返回路径 - (NSString *)path { return self.photoPath; }` 我得到Accessing Unknown photoURL getter method 和Accessin unknown photoPath getter method...请帮忙!
    【解决方案2】:

    如果有其他人在寻找解决方案,我希望这个会有所帮助。

    我已经使用 MWPhotoBrowser 很长时间了。以前,在他们的 .h 文件中,我们可以获取 photoUrl。现在他们移动到 .m 并且我们无法在外部访问该属性。

    由于我使用的是 cocoapods 中的那个库,我也不想修改他们的代码。所以,我创建了这样的类别。我将在 MWPhoto 中单独分配 photoUrl。

    在 MWPhoto+Property.h 中,

    #import <MWPhotoBrowser/MWPhotoBrowser.h>
    
    @interface MWPhoto (Property)
    @property (nonatomic, strong) NSURL *photoUrl;
    @end
    

    在 MWPhoto+Property.m 中,

    #import "MWPhoto+Property.h"
    #import <objc/runtime.h>
    
    @implementation MWPhoto (Property)
    
    static char UIB_PROPERTY_PHOTOURL;
    
    @dynamic photoUrl;
    
    - (void)setPhotoUrl:(NSURL *)photoUrl {
        objc_setAssociatedObject(self, &UIB_PROPERTY_PHOTOURL, photoUrl, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    - (NSURL *)photoUrl {
        return (NSURL *)objc_getAssociatedObject(self, &UIB_PROPERTY_PHOTOURL);
    }
    
    
    @end
    

    然后,当我初始化 MWPhoto 时,我也输入了 photoUrl。

    NSURL *photoUrl = [NSURL URLWithString:messageData.dataDict[@"image"]];
    MWPhoto* photo = [MWPhoto photoWithURL:photoUrl];
    photo.photoUrl = photoUrl; //Due to new implementation in MWPhotoBrowser, property of photoUrl is hidden. So, i put in category
    [self.photos addObject: photo];
    
    //Later, you can access photo.photoUrl in other places.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-29
      • 2012-05-04
      • 1970-01-01
      • 1970-01-01
      • 2010-10-30
      • 2020-03-14
      相关资源
      最近更新 更多