Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何发布图像?_Ios_Objective C_Cocoa Touch - Fatal编程技术网

Ios 如何发布图像?

Ios 如何发布图像?,ios,objective-c,cocoa-touch,Ios,Objective C,Cocoa Touch,我想发布图片,直到它们再次加载。 我用以下代码加载它们: _backgroundImage1.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.jpg", random() % 5]]; 这就是我如何改变阿尔法的 if (backCount == 100) { if (currentBack ==YES) { _backgroundImage1.alpha = _backgroundIma

我想发布图片,直到它们再次加载。 我用以下代码加载它们:

_backgroundImage1.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.jpg", random() % 5]];
这就是我如何改变阿尔法的

if (backCount == 100) {
        if (currentBack ==YES) {

        _backgroundImage1.alpha = _backgroundImage1.alpha - 0.01;
        _backgroundImage2.alpha = _backgroundImage2.alpha + 0.01;

        if (_backgroundImage2.alpha >= 1) {
            backCount = 0;
            currentBack = NO;
            NSString*path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%u", arc4random() % 4] ofType:@"jpg"];
            UIImage *Image = [UIImage imageWithContentsOfFile:path];
            _backgroundImage1.image = Image;

            //_backgroundImage1.image = nil;
            //_backgroundImage1.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.jpg", random() % 5]];

        }
    } else {
使用ARC时:

_backgroundImage1=nil;
假设-backgroundImage1是保存到图像的唯一(强)引用

无弧

[_backgroundImage1.image release];
嗯,假设_backgroundImage1是UIImageView的一个实例或任何仍然希望访问其属性图像的东西,这可能是危险的。所以

_backgroundImage1=nil;
在这里也是个好主意

带弧:

_backgroundImage1=nil;
假设-backgroundImage1是保存到图像的唯一(强)引用

无弧

[_backgroundImage1.image release];
嗯,假设_backgroundImage1是UIImageView的一个实例或任何仍然希望访问其属性图像的东西,这可能是危险的。所以

_backgroundImage1=nil;

在这里也是个好主意

名为的
imageNamed
缓存图像。如果不希望缓存,请使用
imageWithContentsOfFile

正如
imageNamed
文档警告我们的那样:

如果图像文件只显示一次,并且希望确保不会将其添加到系统缓存中,则应使用
imageWithContentsOfFile:
创建图像。这将使您的一次性图像不在系统图像缓存中,有可能改善应用程序的内存使用特性

因此,你将:

NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%ld", random() % 5] ofType:@"jpg"]; 

_backgroundImage1.image = [UIImage imageWithContentsOfFile:path];

您随后说您正在寻找“从一个随机图片淡入另一个随机图片的动画”。在这种情况下,每次更改图像时,您可能只想执行以下操作:

// fade from current image to new image

[UIView transitionWithView:imageView duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
    NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%ld", random() % 5] ofType:@"jpg"];
    imageView.image = [UIImage imageWithContentsOfFile:path];
} completion:^(BOOL finished) {
    // do whatever you want at the end
}];

名为的
图像将缓存图像。如果不希望缓存,请使用
imageWithContentsOfFile

正如
imageNamed
文档警告我们的那样:

如果图像文件只显示一次,并且希望确保不会将其添加到系统缓存中,则应使用
imageWithContentsOfFile:
创建图像。这将使您的一次性图像不在系统图像缓存中,有可能改善应用程序的内存使用特性

因此,你将:

NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%ld", random() % 5] ofType:@"jpg"]; 

_backgroundImage1.image = [UIImage imageWithContentsOfFile:path];

您随后说您正在寻找“从一个随机图片淡入另一个随机图片的动画”。在这种情况下,每次更改图像时,您可能只想执行以下操作:

// fade from current image to new image

[UIView transitionWithView:imageView duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
    NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%ld", random() % 5] ofType:@"jpg"];
    imageView.image = [UIImage imageWithContentsOfFile:path];
} completion:^(BOOL finished) {
    // do whatever you want at the end
}];

ImageName:在本地缓存图像。从文档中:

此方法在系统缓存中查找具有指定名称的图像对象,并返回该对象(如果存在)。如果匹配的图像对象不在缓存中,此方法将从指定的文件加载图像数据,对其进行缓存,然后返回结果对象

如果希望每次都从磁盘加载映像,请使用imageWithContentsOfFile:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"yourImageName" ofType:@"yourImageFileType"];
UIImage *image = [UIImage imageWithContentsOfFile:filePath];

ImageName:在本地缓存图像。从文档中:

此方法在系统缓存中查找具有指定名称的图像对象,并返回该对象(如果存在)。如果匹配的图像对象不在缓存中,此方法将从指定的文件加载图像数据,对其进行缓存,然后返回结果对象

如果希望每次都从磁盘加载映像,请使用imageWithContentsOfFile:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"yourImageName" ofType:@"yourImageFileType"];
UIImage *image = [UIImage imageWithContentsOfFile:filePath];

那么你的问题是什么?你没有图像的内存所有权,所以你不应该发布它?图片仅每分钟使用一次,可能每30秒使用一次这取决于随机性,但我不希望应用程序使用50MB的Ram仅显示4张图片!是的,我已经知道了,但我还没有改变它,因为我第一次想用不太多的内存。我寻找一个从一个随机图片淡入另一个随机图片的动画如果你想在图像之间淡入淡出,那么只需使用
选项
UIViewAnimationOptionTransitionCrossResolve
执行
transitionWithView
。每0.05秒不需要计时器。无需手动调整
alpha
。淡入淡出动画可能比0.05秒计时器更平滑。您所需要做的就是在您想要开始下一个到下一个图像的转换时触发(您可以使用计时器,或者只是从上一个转换的完成块启动它)。查看我的扩展答案。那么你的问题是什么?你没有图像的内存所有权,因此不应该发布它?图片仅每分钟使用一次,可能每30秒使用一次这取决于随机性,但我不希望应用程序使用50MB的Ram仅显示4张图片!是的,我已经知道了,但我还没有改变它,因为我第一次想用不太多的内存。我寻找一个从一个随机图片淡入另一个随机图片的动画如果你想在图像之间淡入淡出,那么只需使用
选项
UIViewAnimationOptionTransitionCrossResolve
执行
transitionWithView
。每0.05秒不需要计时器。无需手动调整
alpha
。淡入淡出动画可能比0.05秒计时器更平滑。您所需要做的就是在您想要开始下一个到下一个图像的转换时触发(您可以使用计时器,或者只是从上一个转换的完成块启动它)。请看我的扩展答案。请耐心等待。你知道吗?是的。但与其他具有自动引用、垃圾收集器和所有工作的编程环境类似,取消所有引用不会立即释放对象。要有耐心,让框架完成它的工作。“若你们想控制内存管理,那个就不要退出。”赫尔曼克尔我理解你们的意图,但你们的退出建议不会释放内存(因为您正在
nil
将指针指向一个
UIImageView
,该视图可能仍然是某个地方的子视图,这不会释放底层
UIImage
,而只是失去了引用的能力