我用UIGraphicsImageRendererFormat替换UIGraphicsBeginImageContext以优化性能并使我的代码现代化。出于某种原因,UIGraphicsImageRendererFormat在我的函数中返回一个空白图像。我一定做错了!
旧功能:(工作正常)
func drawImageInRect(inputImage: UIImage, inRect imageRect: CGRect) -> UIImage {
let renderFormat = UIGraphicsImageRendererFormat.default()
renderFormat.opaque = true
let renderer = UIGraphicsImageRenderer(size: self.size, format: renderFormat)
let scaledImage = renderer.image {
(context) in
inputImage.draw(in: imageRect)
}
return scaledImage
}
新功能:(什么都不画)
public class HeadBoy extends AppCompatActivity {
public static final String SHARED_PREFS = "shareactivity";
public static final String HEAD_BOY_ONE = "count";
public static final String HEAD_BOY_TWO = "counttwo";
public SharedPreferences sharedPreferences ;
public SharedPreferences.Editor editor;
int hb1;
int hb2;
@Override
protected void onResume() {
hideNavigationBar();
super.onResume();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_head_boy);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
final Button hbone = (Button) findViewById(R.id.hbOne);
final Button hbtwo = (Button) findViewById(R.id.hbTwo);
hbone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
hb1 = hb1 + 1;
Intent intent = new Intent(HeadBoy.this,HeadGirl.class);
startActivity(intent);
}
});
hbtwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
hb2 = hb2 + 1;
Intent intent = new Intent(HeadBoy.this,HeadGirl.class);
startActivity(intent);
}
});
hideNavigationBar();
sharedPreferences.edit();
editor.putInt(HEAD_BOY_ONE,hb1);
editor.putInt(HEAD_BOY_TWO,hb2);
editor.commit();
}
public class Results extends AppCompatActivity {
@Override
protected void onResume() {
super.onResume();
hideNavigationBar();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_results);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
sharedPreferences.getInt(HEAD_BOY_ONE,0);
答案 0 :(得分:1)
问题是,在您更新的功能中,您忘记绘制self
,因此您将获得原始图像所在的黑色背景。
func drawImageInRect(inputImage: UIImage, inRect imageRect: CGRect) -> UIImage {
let renderFormat = UIGraphicsImageRendererFormat.default()
renderFormat.opaque = true
let renderer = UIGraphicsImageRenderer(size: self.size, format: renderFormat)
let scaledImage = renderer.image {
(context) in
// Add the following missing line
self.draw(in:(CGRect(x:0.0, y:0.0, width:self.size.width, height:self.size.height)))
inputImage.draw(in: imageRect)
}
return scaledImage
}