0

我想在块中编辑 UIImage 指针,但不允许这样做。

-(void)downloadImage:(NSURL *)url ofPointer:(UIImage *)imagePointer
{
    __weak typeof(self) weakSelf = self;
    [SDWebImageManager.sharedManager downloadWithURL:url
                                             options:0
                                            progress:^(NSUInteger receivedSize, long long expectedSize) {}
                                           completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) {
                                               imagePointer = image;
                                               [weakSelf setNeedsDisplay];
                                           }];

}

我试图用 __block 传递参数,但也不允许这样做。

-(void)downloadImage:(NSURL *)url ofPointer:(__block UIImage *)imagePointer

有没有办法编辑作为参数传递的指针?

4

2 回答 2

0

您需要使用指向指针 ( UIImage **) 的指针,因为图像是不可变对象,因此即使您可以在块内更改它也不会产生您想要的效果。

您可能应该使用委托或回调块将下载的图像从块中传递到将要使用它的实例。

于 2013-05-14T20:09:36.513 回答
0

可以通过将指针传递给要设置的指针来执行此操作:

#import <Foundation/Foundation.h>

@interface Dinsdale : NSObject
- (void)setThisPointer:(NSString *__autoreleasing *)s;
@end

@implementation Dinsdale

- (void)setThisPointer:(NSString *__autoreleasing *)s
{
    void (^b)(void) = ^{
        *s = @"Lemon curry?";
    };

    b();
}

@end

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        Dinsdale * d = [Dinsdale new];
        NSString * s = @"Semprimi!";
        [d setThisPointer:&s];
        NSLog(@"%@", s);    // Prints "Lemon curry?"

    }
    return 0;
}

但最好只使用setter方法:

-(void)downloadImageFromURL:(NSURL *)url
{
    __weak typeof(self) weakSelf = self;
    [SDWebImageManager.sharedManager downloadWithURL:url
                                             options:0
                                            progress:^(NSUInteger receivedSize, long long expectedSize) {}
                                           completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) {
                                               [weakSelf setWhateverImage:image]
                                               [weakSelf setNeedsDisplay];
                                           }];
}
于 2013-05-14T20:11:19.323 回答