如何使用属性编写块定义?

时间:2013-08-13 09:25:50

标签: ios objective-c objective-c-blocks

我正在编写一个用于从照片库中获取图像的课程。 我想要一个从库中返回所选图像的方法。 所以我开始编写一个名为MediaBrowser的类。 我使用了会给出所选图像的块。但我很困惑在哪里写块定义。如果我出错了,请更正代码。

在MediaBrowser.h中

@interface MediaBrowser : NSObject

typedef UIImage* (^MediaBrowserCompletionHandler)(void);

+ (id)sharedInstance;
- (BOOL)startMediaBrowserFromViewController:(UIViewController*)controller
                          completionHandler:(MediaBrowserCompletionHandler)completion;
@end

在MediaBrowser.m中

@interface MediaBrowser () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (nonatomic, strong) MediaBrowserCompletionHandler completionHandler;
@end

@implementation MediaBrowser

static MediaBrowser *sharedMediaBrowser = nil;

+ (id)sharedInstance
{
    if (nil != sharedMediaBrowser) {
        return sharedMediaBrowser;
    }
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedMediaBrowser = [[MediaBrowser alloc] init];
    });
    return sharedMediaBrowser;
}

- (BOOL)startMediaBrowserFromViewController:(UIViewController *)controller completionHandler:(MediaBrowserCompletionHandler)completion
{
    self.completionHandler = [completion copy];
    if (([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum] == NO)
        || (controller == nil))
        return NO;

    UIImagePickerController *mediaUI = [[UIImagePickerController alloc] init];
    mediaUI.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

    // Displays saved pictures from the Camera Roll album.
    mediaUI.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeImage, nil];

    // Hides the controls for moving & scaling pictures, or for
    // trimming movies. To instead show the controls, use YES.
    mediaUI.allowsEditing = NO;
    mediaUI.delegate = self;

    [controller presentModalViewController:mediaUI animated:YES];
    return YES;
}

// UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
    UIImage *imageToUse;

    // Handle a still image picked from a photo album
    if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo) {
        imageToUse = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage];
        // Do something with imageToUse
        if (self.completionHandler) {
            // Pass here UIImage
            self.completionHandler();
        }
    }

    [picker dismissModalViewControllerAnimated:YES];
}

@end

3 个答案:

答案 0 :(得分:2)

我想出了答案。我想将选定的图像从UIImagePickerController发送到调用类。我写错了块。请参阅下面的更正代码。

在MediaBrowser.h中

块声明shoudld是:

typedef void (^MediaBrowserCompletionHandler)(UIImage *selectedImage);

在MediaBroser.m中

呼叫阻止应该是:

if (self.completionHandler) {
     self.completionHandler(imageToUse);
}

答案 1 :(得分:1)

typedef UIImage* (^MediaBrowserCompletionHandler)(void);

@interface MediaBrowser : NSObject

@property (nonatomic,copy) MediaBrowserCompletionHandler handler;

@end

答案 2 :(得分:0)

这应该没问题。

typedef UIImage* (^MediaBrowserCompletionHandler)(void);

@interface MediaBrowser : NSObject
....
@end