gpt4 book ai didi

ios - 如何使用属性编写 block 定义?

转载 作者:行者123 更新时间:2023-11-28 20:15:38 25 4
gpt4 key购买 nike

我正在编写一个从照片库获取图像的类。我想要一个单一的方法来返回从库中选择的图像。所以我开始编写一个名为 MediaBrowser 的类。我使用了将提供选定图像的 block 。但是我很困惑在哪里写 block 定义。如果我出错了,请更正代码。

在 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

最佳答案

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

在 MediaBrowser.h 中

block 声明应该是:

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

在 MediaBroser.m 中

调用 block 应该是:

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

关于ios - 如何使用属性编写 block 定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18205312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com