質問

I am trying to put a few CGImageRefs into an NSArray. It works as it should on the simulator, but not on the device - it crashes at the point where I try to form the array with a EXC_BAD_ACCESS code = 1 error. I tried other things too, like starting with a mutable array but it doesn't work either. The result of typing bt in the console is

(lldb) bt
* thread #1: tid = 0x2403, 0x3a9b25b6 libobjc.A.dylib`objc_msgSend + 22, stop reason = EXC_BAD_ACCESS (code=1, address=0x86ea78f7)
frame #0: 0x3a9b25b6 libobjc.A.dylib`objc_msgSend + 22
frame #1: 0x32a98b5e CoreFoundation`+[__NSArrayI __new:::] + 58
frame #2: 0x00042388 NameOfMyApp

So basically I have a number of images, of letters, and I have a number of layers representing positions in the word that should each cycle through them. For example, layer1 displays letterC, then letterF, then letterP etc, while layer2 displays some other sequence.

I thought I'd load each letter once...

CGImageRef letterH = [UIImage imageWithContentsOfFile:letterHfilename].CGImage;
CGImageRef letterI = [UIImage imageWithContentsOfFile:letterIfilename].CGImage;

...and put the CGImageRefs in an NSArray property, cast to id:

self.titleLetters = @[(__bridge id)letterH, (__bridge id)letterI;

I then later do

layerN.contents = self.titleLetters[i];

but more importantly I have to be able to put them (may times each) in ANOTHER array:

[contentsValuesArray addObject:self.titleLetters[j]];

and then use that array as the values of a keyFrameAnimation:

CAKeyframeAnimation* contentsAnim = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
contentsAnim.values = contentsValuesArray;

All this works fine in the simulator, every time.

As I said, I tried some variations, and the only thing that seems to work is when I form a mutable array with just one letter-imageref in it, using arrayWithObject. Even then, it only works sometimes, and it crashes further down in my programme.

I seem to have read somewhere that the CGImageRefs are not retained, so that they may be released before I even get to use them. Since I began coding after ARC was introduced I know little about releasing, and since it has always worked on the simulator, I figured that didn't apply in my case.

Also, in this reply:

How do I create a mutable array of CGImageRefs?

a person working on Cocoa Frameworks says that it should work although he doesn't give any specifics, so it might be implied.

Any help and suggestions would be much appreciated.

EDIT SOLVED It seems that the problem was indeed that the imagerefs did not exist when I tried to add them to the array, and this was because they had been released already. Apparently they need to be retained using CGImageRetain. It now works, I just need to make sure they are released afterwards.

役に立ちましたか?

解決

The solution was that the imagerefs were being released before I even had time to use them. It seems this happens straight away (at least on the device), unless they are retained using CGImageRetain.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top