所有,我正在研究我认为是一个相当简单的应用程序。我正在使用带有视图的多个视图控制器 - 在其下面有按钮和单个图像视图。 buttonpress事件触发另一个viewcontroller的视图显示。这非常有效。但是,我也想要设置转换动画以模拟翻页。我使用下面的代码来做到这一点。它运行良好,但是,每次使用此方法时,使用的内存都会增加。使用的内存似乎与图像阵列的实际大小断开连接。此外,我从png更改为jpeg(更小的图像),它没有一点点差异。我想过使用.mov,但加载时间非常明显。
请帮忙。我尝试了很多不同的方法来强制垃圾收集。我已经挖掘了有限的文本,并搜索了这个网站无济于事。
以下是代码示例。
public partial class AppDelegate:UIApplicationDelegate {
// This method is invoked when the application has loaded its UI and its ready to run
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
UIApplication.SharedApplication.SetStatusBarHidden( true, true);
// If you have defined a view, add it here:
// window.AddSubview (navigationController.View);
//window.AddSubview(mainController.View);
window.MakeKeyAndVisible ();
coverOpenbtn.TouchUpInside += HandleCoverOpenbtnTouchUpInside;
backBtn1.TouchUpInside += HandleBackBtn1TouchUpInside;
return true;
}
void HandleBackBtn1TouchUpInside (object sender, EventArgs e)
{
this.navView.RemoveFromSuperview();
List<UIImage> myImages = new List<UIImage>();
myImages.Add(UIImage.FromFile("c_1_00011.jpg"));
myImages.Add(UIImage.FromFile("c_1_00010.jpg"));
myImages.Add(UIImage.FromFile("c_1_00009.jpg"));
myImages.Add(UIImage.FromFile("c_1_00008.jpg"));
myImages.Add(UIImage.FromFile("c_1_00007.jpg"));
myImages.Add(UIImage.FromFile("c_1_00006.jpg"));
myImages.Add(UIImage.FromFile("c_1_00005.jpg"));
myImages.Add(UIImage.FromFile("c_1_00004.jpg"));
myImages.Add(UIImage.FromFile("c_1_00003.jpg"));
myImages.Add(UIImage.FromFile("c_1_00002.jpg"));
myImages.Add(UIImage.FromFile("c_1_00001.jpg"));
myImages.Add(UIImage.FromFile("c_1_00000.jpg"));
//myImages.Add(UIImage.FromFile("c_1_00012.jpg"));
var myAnimatedView = new UIImageView(window.Bounds);
myAnimatedView.AnimationImages = myImages.ToArray();
myAnimatedView.AnimationDuration = 1; // Seconds
myAnimatedView.AnimationRepeatCount = 1;
myAnimatedView.StartAnimating();
window.AddSubview(myAnimatedView);
}
void HandleCoverOpenbtnTouchUpInside (object sender, EventArgs e)
{
this.coverView.AddSubview(navView);
List<UIImage> myImages = new List<UIImage>();
myImages.Add(UIImage.FromFile("c_1_00000.jpg"));
myImages.Add(UIImage.FromFile("c_1_00001.jpg"));
myImages.Add(UIImage.FromFile("c_1_00002.jpg"));
myImages.Add(UIImage.FromFile("c_1_00003.jpg"));
myImages.Add(UIImage.FromFile("c_1_00004.jpg"));
myImages.Add(UIImage.FromFile("c_1_00005.jpg"));
myImages.Add(UIImage.FromFile("c_1_00006.jpg"));
myImages.Add(UIImage.FromFile("c_1_00007.jpg"));
myImages.Add(UIImage.FromFile("c_1_00008.jpg"));
myImages.Add(UIImage.FromFile("c_1_00009.jpg"));
myImages.Add(UIImage.FromFile("c_1_00010.jpg"));
myImages.Add(UIImage.FromFile("c_1_00011.jpg"));
//myImages.Add(UIImage.FromFile("c_1_00012.jpg"));
var myAnimatedView = new UIImageView(window.Bounds);
myAnimatedView.AnimationImages = myImages.ToArray();
myAnimatedView.AnimationDuration = 1; // Seconds
myAnimatedView.AnimationRepeatCount = 1;
opened++;
}
myAnimatedView.StartAnimating();
window.AddSubview(myAnimatedView);
}
答案 0 :(得分:2)
这里有一些提示(只需阅读代码):
图像加载到内存后,JPEG和PNG之间没有区别。格式仅在存储图像时才重要,不显示。一旦加载(和解压缩),它们将花费一些(Width * Height * BitCount)内存。
考虑缓存图片并加载它们,只有它们不可用。 GC将决定何时收集它们(可以同时存在多个副本)。现在你只需要加载每个图像两次(并使用单独的数组进行排序)。
即使您将其缓存,也可以随时按需清除它们,例如如果iOS警告你内存很低。覆盖ReceiveMemoryWarning以清除列表(或更好的数组)。
如果可以避免,请不要调用ToArray(如示例代码)。如果您知道有多少图像,只需创建具有正确大小的数组(并同时缓存两个数组;-)。它会减少(一点点)分配;
即使考虑缓存'myAnimatedView'UIImageView(如果上述内容没有帮助)
对他人有所帮助,一个接一个地尝试,并告诉我们对你有什么帮助: - )
答案 1 :(得分:0)
图像是为了“动画”页面翻转...这是通过应用程序导航?
E.g。你从“主页”页面开始,按一个按钮然后它会动画一页翻到你应用程序的下一个屏幕?
我认为你会更好地使用CoreGraphics来尝试实现这种效果,它会更有效地记忆,并且它看起来也会好得多。 Objective-C中有一些项目可以帮助您入门,例如Tom Brow's excellent Leaves project.
答案 2 :(得分:0)
好的,这是我找到的最佳解决方案,不会使硬件崩溃,并且通常对其他任务有用。
这是按下按钮的处理程序中的代码,每个NavImage都是我在界面构建器中的相同视图下构建的UIImage。我刚开始将alpha变为0,然后逐个点亮......
NSTimer.CreateScheduledTimer(.1,delegate { navImage1.Alpha = 1; NSTimer.CreateScheduledTimer(.1,delegate { navImage2.Alpha = 1;
NSTimer.CreateScheduledTimer(.05,delegate { navImage3.Alpha = 1;
NSTimer.CreateScheduledTimer(.05,delegate { navImage4.Alpha = 1;
NSTimer.CreateScheduledTimer(.05,delegate { navImage5.Alpha = 1;
NSTimer.CreateScheduledTimer(.05,delegate { navImage6.Alpha = 1;
NSTimer.CreateScheduledTimer(.05,delegate { navImage7.Alpha = 1;
NSTimer.CreateScheduledTimer(.05,delegate { navImage8.Alpha = 1;
NSTimer.CreateScheduledTimer(.05,delegate { navImage9.Alpha = 1;
NSTimer.CreateScheduledTimer(.05,delegate { navImage.Alpha = 1; });});});});});});});});});});