I currently am using the code I found on an online tutorial to have the iPhone user select an image from their camera roll. Setting the image to the UIImageView works fine, but then I try to call a function on the image (every time a slider is moved). It turns out that the operating system is not holding onto the image, so when I try to access it (or the caching variable I made), it doesn’t work (because the address gets set to 0x0000000).
If UIImage conformed to NSCopying, this would be simple. But, it doesn’t. So, how do I copy an image so the operating system doesn’t delete it?
Possible Duplicates
- How do I make an exact copy of a UIImage returned from a UIImagePickerController?
- Making deep copy of UIImage
EDIT:
The slider, when changed, calls this function:
- (IBAction)samplingSliderChanged:(id)sender {
NSLog(@"%@", self.imageStay);
float sample = self.samplingSlider.value * 0.6 + 0.2;
self.samplingRateText.text = [NSString stringWithFormat:@"%.0f%%", sample*100];
//self.imageView.image = [self.brain sampleImage:self.imageStay atRate:sample];
self.imageView.image = [self.brain sampleImage:self.imageStay.copy atRate:sample];
NSLog(@"self.imageStay: %@, self.imageStay.copy: %@", self.imageStay, self.imageStay.copy);
}
The first time the slider is moved, my self.imageStay (the cache variable) has an address of 0x0000.
My show-the-camera-roll function is below:
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = info[UIImagePickerControllerMediaType];
UIImage * image;
[self dismissViewControllerAnimated:YES completion:nil];
image = info[UIImagePickerControllerOriginalImage];
image = [self imageWithImage:image scaledToSize:CGSizeMake(256, 256)];
self.imageView.image = image;
if (_newMedia)
UIImageWriteToSavedPhotosAlbum(image,
self,
@selector(image:finishedSavingWithError:contextInfo:),
nil);
self.imageView.image = image;
self.imageStay = image
}
In here, the cache variable has the same memory address as image, and my guess is that this later gets deleted.
You shouldn’t be worrying about NSCopying, or making a copy of a UIImage, this isn’t your problem. You are losing the reference to the image in your imageView. The OS is holding onto the image, if not it would disappear from your imageView. I expect you just aren’t referring to it correctly. Perhaps you should show what you mean by a ‘caching variable’…
In any case a reliable way to get to the image currently displayed in your imageView is to obtain it from the imageView’s image property, as
self.imageView.image.update
If
self.imageView.imagemight be subject to filtering, and you also need to keep a reference to the original image (eg in self.imageStay) you should declare the imageStay property asIf you have done this correctly, and still have a nil reference in self.imageStay, step through your code in the debugger to see what is going on here:
self.imageStayandself.imageView.imageshould now both have a reference to the same object. By the way, you second assignment ofself.imageView.image = imagehere is redundant.Then in
- (IBAction)samplingSliderChanged:(id)senderyou should find those references persisting.Your problem might be caused if you have declared your
@property imageStayas a weak reference. It should bestrongto ensure that it persists.