2013-05-14 28 views
0

我想在塊中編輯UIImage指針,但不允許。在Objective-C塊中編輯參數

-(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

有什麼辦法來編輯作爲參數傳遞的指針?

回答

0

您需要使用指向指針的指針(UIImage **),因爲圖像是不可變的對象,所以即使您可以在塊內部更改它,也不會產生所需的效果。

你應該使用委託或回調塊將下載的圖像從塊中傳遞給將要使用它的實例。

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]; 
              }]; 
} 
+0

由於喬希, 很好的答案,但我不能通過一個屬性作爲參數 ''[self downloadImage:imageURL ofPointer:&_ imgUserPost1];' ERR:Passing add非本地對象的RESS爲回寫__autoreleasing參數 或 '[自downloadImage:IMAGEURL ofPointer:self.imgUserPost1]' ERR:屬性表達式的地址請求 – suleymancalik

+0

是的,不這樣做的。使用塊中的setter。 –

+0

我也不能這樣做,因爲哪個屬性設置不確定。這就是爲什麼我把它作爲一個論點。 – suleymancalik