如何在for循环之外返回public abstract class FilteringDemoDelegate
{
public abstract void ImageViewControllerDidFinish(UIImage filteredImage);
}
public class ImageViewController : UIViewController
{
UIImage sourceImage, displayImage, resultImage;
public FilteringDemoDelegate filteringDelegate;
UIButton saveButton, resetButton;
UIImageView imageView;
UISlider sliderC, sliderB;
UILabel labelC, labelB;
public ImageViewController(UIImage image)
{
sourceImage = image;
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
Title = "Settings";
View.BackgroundColor = UIColor.White;
resetButton = UIButton.FromType(UIButtonType.RoundedRect);
resetButton.Frame = new CGRect(110, 40, 90, 40);
resetButton.SetTitle("Reset", UIControlState.Normal);
resetButton.SetTitleColor(UIColor.FromRGB(11, 132, 66), UIControlState.Normal);
resetButton.TouchUpInside += (sender, e) => {
sliderB.Value = 0;
sliderC.Value = 1;
HandleValueChanged(sender, e);
};
View.Add(resetButton);
saveButton = UIButton.FromType(UIButtonType.RoundedRect);
saveButton.Frame = new CGRect(210, 40, 90, 40);
saveButton.SetTitle("Save", UIControlState.Normal);
saveButton.SetTitleColor(UIColor.FromRGB(11, 132, 66), UIControlState.Normal);
saveButton.TouchUpInside += (sender, e) => {
if (resultImage != null)
{
ImageEditingViewController(resultImage);
}
else
{
ImageEditingViewController(sourceImage);
}
};
View.Add(saveButton);
labelC = new UILabel(new CGRect(10, 100, 90, 20));
labelB = new UILabel(new CGRect(10, 140, 90, 20));
labelC.Text = "Contrast";
labelB.Text = "Brightness";
View.Add(labelC);
View.Add(labelB);
sliderC = new UISlider(new CGRect(100, 100, 210, 20));
sliderB = new UISlider(new CGRect(100, 140, 210, 20));
sliderC.MinValue = 0;
sliderC.MaxValue = 4;
sliderC.Value = 1;
sliderB.MinValue = -1;
sliderB.MaxValue = 1;
sliderB.Value = 0;
// update the image in 'real time' as the sliders are moved
sliderC.ValueChanged += HandleValueChanged;
sliderB.ValueChanged += HandleValueChanged;
View.Add(sliderC);
View.Add(sliderB);
imageView = new UIImageView(new CGRect(10, 200, View.Bounds.Width - 20, View.Bounds.Height - 200));
displayImage = sourceImage;
imageView.Image = displayImage;
View.Add(imageView);
}
void HandleValueChanged(object sender, EventArgs e)
{ // use the low-res version
imageView.Image = AdjustImage(displayImage);
if (resultImage != null)
{
resultImage.Dispose();
}
resultImage = imageView.Image;
}
CIContext context;
CIColorControls colorCtrls; //CIFilter
UIImage AdjustImage(UIImage image)
{
if (colorCtrls == null)
colorCtrls = new CIColorControls() { Image = CIImage.FromCGImage(image.CGImage) };
else
colorCtrls.Image = CIImage.FromCGImage(image.CGImage);
if (context == null)
context = CIContext.FromOptions(null);
colorCtrls.Contrast = (float)Math.Round(sliderC.Value, 1);
colorCtrls.Brightness = (float)Math.Round(sliderB.Value, 1);
using (var outputImage = colorCtrls.OutputImage)
{
var result = context.CreateCGImage(outputImage, outputImage.Extent);
return UIImage.FromImage(result);
}
}
public void ImageEditingViewController(UIImage filteredImage)
{
if (filteringDelegate != null)
{
filteringDelegate.ImageViewControllerDidFinish(filteredImage);
}
DismissViewController(false, null);
}
}
的值。我需要进行api调用stuff
次,并且每次都存储结果。答案中可能存在重复的问题,但我无法找到答案。
i
我试过让这个函数只进行1次调用,然后在另一个调用此函数的函数中循环,但我得到function getResponse() {
var stuff = []
for (let i = 0; i < length; i++) {
axios.get(url + i)
.then(res => res.forEach(item => {
stuff.push(item)
}))
}
// How do I return stuff from this function ?
}
console.log(getResponse())
。
答案 0 :(得分:1)
如何从此功能返回内容?
你做不到。它就像“我想得到明天的报纸”。这也行不通。您必须等到第二天才能阅读报纸,或者在您的情况下,直到所有数据都到达浏览器。您不能直接返回数据,而是一个 Promise ,它将在某些时候提供数据。在您的情况下,您可以使用Promise.all
将一组promises统一为一个解析为数组的promise:
function getResponse() {
const promises = [];
for (let i = 0; i < length; i++) {
promises.push(axios.get(url + i));
}
return Promise.all(promises)
.then(results => [].concat(...results));
}
现在如果你这样做:
console.log(getResponse())
你得到了承诺。要获取实际数据,您必须等到数据到达:
getResponse().then(console.log);