English 中文(简体)
目标C:更新统一分类的方法
原标题:Objective C: Method to update UIImage

请考虑以下方法:

+(void)getGravatarFromEmail: (NSString *)email ofSize:(NSString *)size toImg:(UIImage *)myImg
{
    NSString *hash = [md5 getMD5Hash:email];  // external method to calculate md5 hash
    NSURL *gravatarUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://gravatar.com/avatar/%@?s=%@",hash,size]];

    ASIHTTPRequest *request = [[ASIHTTPRequest alloc]initWithURL:gravatarUrl];
    [request setCompletionBlock:^(void) {
        NSData *data = [request responseData];
        myImg = [UIImage imageWithData:data];  // ERROR HERE!!
    }];
    [request setDelegate:self];
    [request startAsynchronous];
}

这种方法简单地要求格拉瓦塔尔找回图像。 然后,我将用光彩的形象更新我的记忆。

However, I m having errors trying to assign the image into myImg as commented in the code. The error is:

不可转让

这对我有什么影响?

最佳回答
+(void)getGravatarFromEmail:(NSString *)email ofSize:(NSString *)size 
           toImg:(UIImage *)myImg {
    NSString *hash = [md5 getMD5Hash:email];  // external method to calculate md5 hash
    NSURL *gravatarUrl = [NSURL URLWithString:[NSString
           stringWithFormat:@"http://gravatar.com/avatar/%@?s=%@",hash,size]];

    ASIHTTPRequest *request = [[ASIHTTPRequest alloc]initWithURL:gravatarUrl];

    __block UIImage *blockImage = myImg;

    [request setCompletionBlock:^(void) {
        NSData *data = [request responseData];
        blockImage = [UIImage imageWithData:data];  // ERROR HERE!!
    }];
    [request setDelegate:self];
    [request startAsynchronous];
}

在谈到区块时,我仍是一个新奇,但我认为,上述修改应当使你的法典能够编纂。 尽管如此,我不敢肯定你的方法是妥善使用。 首先,这种方法被混为一谈(至少是大多数退伍军人)。 如果将<代码>(撤销)改为get,那么看来应当有点人到像样的UIImage:

+(void)getGravatarFromEmail:(NSString *)email ofSize:(NSString *)size 
               toImg:(UIImage **)myImg;

我认为,如果你重回真空,那是你能够再做任何事情的唯一途径。

这也是一种分类方法,你重新制定<代码>。 ASIHTTPRequest 代表您的班子,而不是您的物体的事例?

无论如何,就你正在发现的错误而言:

否则,我认为区块只能读取变数。 为了能够修改栏目中的<代码>myImg(或blockImage>变量,你需要用<代码>_block的关键词预先确定变量。 (如上所述,由于上述原因,我并非所有人都能确保能够从路障内改变这一变量的好东西。)

问题回答

暂无回答




相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

Iphone NSTimer Issue

Hi I am new to objective c. I am trying to make an app for iphone. I have a button on my view, and the click on which the function playSound is called. This is working properly. It does plays the ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Xcode open two editor windows with same file

Is it possible to open the same file in two separate windows in Xcode. I can open a file in one window and the same file in the main Xcode editor window, but I wanted two separate fulltime editor ...

Forcing code signing refresh in Xcode

In our environment, we share resources across multiple projects and platforms. When building for iPhone, only a subset of those resources are needed. Since that subset is still considerable, we have ...

热门标签