Ios 使用iCarousel获取错误
这是为我的Ios 使用iCarousel获取错误,ios,objective-c,automatic-ref-counting,Ios,Objective C,Automatic Ref Counting,这是为我的iCarouselViewController.m - (void)dealloc { //it's a good idea to set these to nil here to avoid //sending messages to a deallocated viewcontroller carousel1.delegate = nil; carousel1.dataSource = nil;
iCarouselViewController.m
- (void)dealloc
{
//it's a good idea to set these to nil here to avoid
//sending messages to a deallocated viewcontroller
carousel1.delegate = nil;
carousel1.dataSource = nil;
carousel2.delegate = nil;
carousel2.dataSource = nil;
[carousel1 release];
[carousel2 release];
[items1 release];
[items2 release];
[super dealloc];
}
我在说“发布”不可用:在自动引用中不可用 计数模式
ARC禁止“释放”的显式消息发送
“发布”不可用:在自动引用中不可用 计数模式
ARC禁止“释放”的显式消息发送
“发布”不可用:在自动引用中不可用 计数模式
ARC禁止“释放”的显式消息发送
“发布”不可用:在自动引用中不可用 计数模式
ARC禁止“释放”的显式消息发送
ARC禁止“dealloc”的显式消息发送
以及此代码中的错误
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UILabel *label = nil;
//create new view if no view is available for recycling
if (view == nil)
{
view = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 200.0f)] autorelease];
((UIImageView *)view).image = [UIImage imageNamed:@"page.png"];
view.contentMode = UIViewContentModeCenter;
label = [[[UILabel alloc] initWithFrame:view.bounds] autorelease];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = UITextAlignmentCenter;
label.font = [label.font fontWithSize:50];
[view addSubview:label];
}
else
{
label = [[view subviews] lastObject];
}
说“自动释放”不可用:在自动引用中不可用 计数模式
ARC禁止显式发送 “自动释放”不可用:“自动释放”在中不可用 自动参考计数模式
ARC禁止显示消息 发送“自动释放”
如何清除此错误。
更新
谢谢你的回答,我刚才有4个错误,说使用了未声明的标识符imageArray1。我知道这正在发生。我只是不明白“我假设你只是在使用应用程序的捆绑包,我们有两个NSString数组,它们引用每个图像:imageArray1和imageArray2。”下面是我的一个保存代码,并为我的一个目录创建目录。注意:我只有一个名为allImagesArray的NSMutableArray,我在头文件中声明了它
NSArray *directoryNames = [NSArray arrayWithObjects:@"Apple",nil];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
for (int i = 0; i < [directoryNames count] ; i++) {
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:[directoryNames objectAtIndex:i]];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil]; //Create folder
NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:@"Tops"]; NSData *imageData = UIImagePNGRepresentation(captureImage.image);
time_t unixtime = (time_t)[[NSDate date]timeIntervalSince1970];
NSString *timestamp = [NSString stringWithFormat:@"%ldTopsImage.PNG",unixtime];
NSString *filePath = [folderPath stringByAppendingPathComponent:timestamp];
[imageData writeToFile:filePath atomically:YES];
}
}
这不是iCarousel的问题。您在代码中使用的语句有
release
,autorelease
。移除它。在ARC中,您不需要手动执行内存管理。这就是ARC存在的原因
更新:根据您的评论,您在显示多个传送带的图像时面临问题。 我假设您使用的是两个iCarousel对象。让我们将它们命名为
carousel1
和carousel2
。
此外,您似乎正在使用沙盒来保存图像。如果是这种情况,则必须使用NSFileManager
从沙箱中获取图像。您需要继续研究如何做到这一点,但在这种情况下,iCarousel的代码也将或多或少保持不变。在这里,为了简单起见,我假设您正在使用应用程序包,我们有两个NSString
数组,它们引用每个图像:imageArray1
和imageArray2
在viewDidLoad
中,将每个转盘的delegate
和数据源对象设置为self
carousel1.delegate = self;
carousel1.dataSource = self;
carousel2.delegate = self
carousel2.dataSource = self;
相应地实施数据源方法:
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
//return the total number of items in the carousel
if (carousel == carousel1)
{
return [imageArray1 count];
}
else
{
return [imageArray2 count];
}
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UILabel *label = nil;
//create new view if no view is available for recycling
if (view == nil)
{
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 200.0f)];
UIImage *image;
if (carousel == carousel1)
{
image = [UIImage imageWithContentsOfFile:[imageArray1 objectAtIndex:index]];
((UIImageView *)view).image = image;
}
else
{
image = [UIImage imageWithContentsOfFile:[imageArray2 objectAtIndex:index]];
((UIImageView *)view).image = image;
}
}
return view;
}
实际上这很简单,您在创建XCode项目时勾选了“使用自动引用计数”(ARC)框。要解决这个问题,只需转到您的目标(我假设是伊卡洛斯),然后执行“buldsettings”-->objective-c automaticrefcount:否
然后编译器就可以工作了。ARC所做的是自动为您完成这些工作,所以您不必编写自动发布和发布声明,但如果您需要它们,请按照我的建议执行。否则请删除这些release和autorelease语句。这是因为您在项目中使用了ARC,而此处显示的代码没有。要在这些类中禁用它,请添加
-fno objc arc
。您可以通过进入“目标构建阶段”选项卡来执行此操作。在“编译源代码”组中,双击文件(类)并添加-fno objc arc
标志
或者你可以删除所有发布消息。谁说iCarousel不会?我使用的是它的1.7.6版本,它确实支持ARC。非常简单,您只需要避免发送编译器警告的消息。当然,可以使用编译器标志告诉编译器此文件不使用ARC来解决此问题。但是,在我看来,如果您在某些文件中使用ARC,而在某些文件中使用非ARC,那么这将更加混乱,维护代码也更加困难。我们只能采取一种办法。如果问题是由于在非ARC中编译的库引起的,那么使用编译器标志是明智的,但这里的问题只是因为OP正在使用带有release/autorelease的代码。谢谢,所有内容都已清除,但我找不到将图片导入旋转木马的位置,因为我将从我的应用程序目录导入并显示到两个旋转木马中。如果图像存储在应用程序包中,则只需创建一个包含其名称的数组,并使用viewForItemAtIndex方法。替换此行:((UIImageView*)视图.image=[uiImageName:@“page.png”];带有((UIImageView*)视图。图像=[UIImageName:[createdArray objectAtIndex:index]];很抱歉问了很多问题。新的旋转木马。我已将我的照片保存在目录旁边的子目录中。当然,我已经为它创建了一个数组。但是,我想为顶部分配一个子目录,为底部分配一个不同的子目录。我使用了你的代码,但什么也没发生。我没有错误。您正在沙盒中保存图像。检索每个图像路径(将在NSString中),并使用这些字符串创建imageArray1,以将图像加载到转盘1中,并相应地创建imageArray2。请参阅我的编辑,了解如何从检索路径加载这些图像,并在iCarousSorry中显示我对imageArray1和imageArray2的理解有误。在我的例子中,我不认为我必须使用数组。我创建了两个数组,因为你提到了两个旋转木马。现在,如何将图像从单个阵列加载到两个旋转木马中?如果你想使用单个数组,你要么说数组中的前十个图像是用于carouse1的,而所有其他图像是用于Carousel2的。这对我来说似乎不实际。所以我创建了两个数组。要学习的是如何从映像路径创建这两个数组。正如我说的,你需要学习和理解NSFil
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
//return the total number of items in the carousel
if (carousel == carousel1)
{
return [imageArray1 count];
}
else
{
return [imageArray2 count];
}
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UILabel *label = nil;
//create new view if no view is available for recycling
if (view == nil)
{
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 200.0f)];
UIImage *image;
if (carousel == carousel1)
{
image = [UIImage imageWithContentsOfFile:[imageArray1 objectAtIndex:index]];
((UIImageView *)view).image = image;
}
else
{
image = [UIImage imageWithContentsOfFile:[imageArray2 objectAtIndex:index]];
((UIImageView *)view).image = image;
}
}
return view;
}