Swift:将UIImage设置为背景

时间:2015-04-26 23:43:51

标签: ios swift uiimage uicolor

我在viewDidLoad()中的行抱怨没有!在以下声明中:

 view.backgroundColor = UIColor.colorWithPatternImage(UIImage(named: "background.jpg")!)

但是,当我按照建议将其更改为此时:

public class MyOkHttpInterceptor implements Interceptor {

@Override
public Response intercept(Chain chain) throws IOException {
    Request originalRequest = chain.request();
    if (!"/posts".contains(originalRequest.url()) ) {
        return chain.proceed(originalRequest);
    }

    String token = // get token logic 

    Request newRequest = originalRequest.newBuilder()
        .header("X-Authorization", token)
        .build();

    return chain.proceed(newRequest);
}

[...]

OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.networkInterceptors().add(new MyOkHttpInterceptor());
OkClient okClient = new OkClient(okHttpClient);
YourApi api = new RestAdapter.Builder()
            .setEndpoint(url)
            .setClient(okClient)
            .build()
            .create(YourApi.class);

它出现以下错误:

  

致命错误:在解包可选值时意外发现nil

再次,非常感谢任何帮助!

1 个答案:

答案 0 :(得分:5)

将您的代码更改为:

if let image = UIImage(named: "background.jpg") {
    view.backgroundColor = UIColor.colorWithPatternImage(image)
} else {
    println("There was no such image as background.jpg")
}

这样你就不会崩溃,println将登录到控制台并向你证明图像不存在 - 当然,直到它。