Trouble updating/setting the image in a UIImageView - uiimageview

I'm trying to call a UIImagePickerController and use the info that it gets to update a UIImageView. I can't get the UIImage from the info to the UIImageView.
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage* originalImage = nil;
originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
[myImage setImage:originalImage]; //also tried myImage.image = originalImage
NSLog(#"originalImage: %#\n", originalImage);
NSLog(#"userImage.image: %#\n", myImage.image);
[self dismissViewControllerAnimated:YES completion:nil];
}
When I run this code the UIImageView stays blank.
I get a console output of:
originalImage: <UIImage: 0x80e0f90>
userImage.image: (null)

myImage is null, so any call on it has no effect, something should be assigned to it beforehand (myImage = ... or self.myImage = ...)

Related

Unable to see UIImage once stored and sent to Server form CoreData?

The first part of the code is where the camera button is pressed but not before checking if a textfield has been filled, this then checks camera type. All normal here me thinks.Once the picture has been taken I then resize the image so that its of smaller size then convert it to a NSString to which then the NSString data is stored in CoreData and then sent off to a server to be shown on screen.
This is the camera press button
-(IBAction)takePicture:(UIButton*)sender {
if (printedname.text.length ==0){
UIAlertView*alert=[[UIAlertView alloc]initWithTitle:
#"Please enter Name"message:#""delegate:self cancelButtonTitle:
#"Dismiss"otherButtonTitles:nil];[alertshow];return;
}
UIImagePickerController *pictureTaker = [[UIImagePickerController alloc] init];
pictureTaker.delegate = self;
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera]) {
pictureTaker.sourceType = UIImagePickerControllerSourceTypeCamera;
} else if([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeSavedPhotosAlbum]){
pictureTaker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
[self presentViewController:pictureTaker animated:YES completion:nil];}
-(void)imagePickerController:(UIImagePickerController *)pictureTaker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(#"%#", info);
NSString * mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if([mediaType isEqual:(__bridge NSString *)kUTTypeImage]){
[pictureTaker dismissViewControllerAnimated:YES completion:nil];
base64Image = [info objectForKey:UIImagePickerControllerEditedImage];
base64Image = [info objectForKey:UIImagePickerControllerOriginalImage];
CGSize newSize = CGSizeMake(100.0f,60.0f);
UIGraphicsBeginImageContext(newSize);
[base64Image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
base64Image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *data = UIImageJPEGRepresentation(base64Image, 1.0);
base64string = [data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
NSLog(#" image (%#), width: %.0f, height: %.0f, scale: %0.2f, string len %lu",base64Image,base64Image.size.width,
base64Image.size.height, base64Image.scale,(unsigned long)base64string.length);
}
UIImageWriteToSavedPhotosAlbum(base64Image, self, NULL , NULL);
}
It seem to send ok save but kind of get's lost half way through, can anyone help as Iv been >trying to sort this out for days now, Iv included below the console output for you to see >aswell, thanks to anyone that can help.
UIImagePickerControllerMediaType = "public.image";
UIImagePickerControllerOriginalImage = "<UIImage: 0x17056260> size
{ 2448, 3264 }
orientation 3 scale 1.000000";
}
I managed to fix the problem with the code below:
if (self.base64Image!= nil){
NSString *strImageData = [self.base64Image stringByReplacingOccurrencesOfString:#"+" withString:#"%2B"];
NSString *param =[NSString stringWithFormat:#"photo=%#", strImageData];
[values addObject:param];
}

Cocos2d V3 can't switch scenes after using the UIImagePickerController Class

I'm having a little trouble switching scenes (or even displaying sprites) on my CCscene UserLevelCreation, but it only happens after i use the UIImagePickerController to take/accept a picture, And then when I try to switch scenes, It crashes with the error:
"Could not attach texture to framebuffer"
this is the code I use to take a picture:
-(void)takePhoto{
AppController *appdel = (AppController*) [[UIApplication sharedApplication] delegate];
#try {
uip = [[UIImagePickerController alloc] init] ;
uip.sourceType = UIImagePickerControllerSourceTypeCamera;
uip.allowsEditing = YES;
uip.delegate = self;
}
#catch (NSException * e) {
uip = nil;
}
#finally {
if(uip) {
[appdel.navController presentModalViewController:uip animated:NO];
}
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSString * LevelImage;
LevelImage=[info objectForKey:UIImagePickerControllerOriginalImage];
AppController *appdel = (AppController*) [[UIApplication sharedApplication] delegate];
[appdel.navController dismissModalViewControllerAnimated:YES];
[NSThread detachNewThreadSelector:#selector(writeImgToPath:) toTarget:self withObject:LevelImage];
}
And the WriteImageToPath function:
-(void)writeImgToPath:(id)sender
{
UIImage *image = sender;
NSArray *pathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
CGSize size;
int currentProfileIndex = 1;
NSString *path = [[pathArr objectAtIndex:0]
stringByAppendingPathComponent:[NSString stringWithFormat:#"CustomLevel_%d.png",currentProfileIndex]];
size = CGSizeMake(1136, 640);
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, 1136, 640)];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
CCLOG(#"saved...");
CGRect r = CGRectMake(0, 0, 1136, 640);
UIGraphicsBeginImageContext(r.size);
UIImage *img1;
[image drawInRect:r];
img1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(img1, nil, nil, nil);
currentProfileIndex++;
[[CCDirector sharedDirector] replaceScene:[LevelEditor Scene]
withTransition:[CCTransition transitionPushWithDirection:CCTransitionDirectionLeft duration:1.0f]];
}
-UPDATE----------------------------------------------------------------------------------
I just tried switching scenes in different places in the code, and I can switch scenes freely until the Function:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
I think that function might be the problem.
So far, I have tried to:
1.switch scenes before taking the picture, and that works fine
2.I read that it might be because i was resizing the image, but commenting that out made no difference.
3.I've also added breakpoints before the scene switch, and the scene isn't nil
4.Finally i tred dismissing the uip UIImagePickerController, but that made no difference either.
Sorry for the long post /: if anyone knows whats going on any help would be really great.
Ok so I found the problem to be this line:
[NSThread detachNewThreadSelector:#selector(writeImgToPath:) toTarget:self withObject:LevelImage];
}
Apparently some Parts of Uikit are not thread-friendly, so I'm now using just
[self writeImgToPath];
And it works, hope this helps anyone with the same problem.

UIImagePickerController not working

I am using xcode 4.5.2 and i want to use the UIImagePickerController on my navigationBar based app, everything works perfect unless the image keeps blank after the user picks the image.
the user should tab on a button->choose camera or existing from ActionSheet->Pick the image, and then .. nothing is showing !!
i've added the delegates <UINavigationControllerDelegate,UIActionSheetDelegate, UIImagePickerControllerDelegate> and linked everything in IB properly .. am very sure of that except am not sure about this method:
- (void) imagePickerController:(UIImagePickerController *)thePicker didFinishPickingMediaWithInfo:(NSDictionary *)imageInfo{
[[imgPicker parentViewController] dismissViewControllerAnimated:YES completion:nil];
UIImage *img = [imageInfo objectForKey:#"UIImagePickerControllerEditedImage"];
currentImg.image = nil;
self.currentImg.image = img;
}
i used this also:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
image.image = img;
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
and nothing happened ..
SOLVED
missed to add this line:
self.imgPicker.delegate = self;
in viewDidLoad :)
Try using [info objectForKey:UIImagePickerControllerOriginalImage]; instead of using the edited image. Usually you check to see if the edited image exists, use it if it does, and if not then use the original image.
Something like this:
- (void) imagePickerController:(UIImagePickerController *) picker didFinishPickingMediaWithInfo:(NSDictionary *) info {
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
UIImage *originalImage, *editedImage, *imageToSave;
// Handle a still image capture
if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo) {
editedImage = (UIImage *) [info objectForKey:
UIImagePickerControllerEditedImage];
originalImage = (UIImage *) [info objectForKey:
UIImagePickerControllerOriginalImage];
if (editedImage) {
imageToSave = editedImage;
} else {
imageToSave = originalImage;
}
}
self.currentImg.image = imageToSave;
}

Posting images from camera roll to facebook in xcode 4.5

I've got some functionality set up. But I'm lost on where to go from here. I can probably figure out what to do with facebook once I know how to actually use the images. I tried saving the image into NSDictionary and then redirecting to a different View Controller, but it won't let me redirect from within the imagePickerController method. So anybody have any idea how to use the image selected from camera roll?
My next idea was to save it in the NSDictionary and then just have a statement checking to see if the NSdictionary value changed but that's not an efficient way of doing it at all.
EDITED below to include the answer provided, but nothing happens, no image displays or anything. What am I missing?
- (void) useCameraRoll
{
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [NSArray arrayWithObjects:
(NSString *) kUTTypeImage,
nil];
imagePicker.allowsEditing = NO;
[self presentModalViewController:imagePicker animated:YES];
[imagePicker release];
newMedia = NO;
}
}
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info
objectForKey:UIImagePickerControllerMediaType];
[self dismissModalViewControllerAnimated:YES];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage *image = [info
objectForKey:UIImagePickerControllerOriginalImage];
NSLog(#"image:%#",image);
displayPhoto.image = image;
[displayPhoto setFrame:CGRectMake(0, 0, 320, 480)];
[[self view] addSubview:displayPhoto];
}
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{
// Code here to support video if enabled
}
}
Assuming you have called this from a view controller with a button that you have created, why not just add a UIImageView onto your view controller? Call it myPhotoImageView or something like that and then in the didFinishPickingMediaWithInfo method, just add the following line of code after
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
as
myPhotoImageView.image = image;
To size the image view so that the aspect looks nice do this.
CGSize size = image.size;
CGRect photoFrame;
if (size.width > size.height)
{
// Landscape
CGFloat scaleFactor = 320 / size.width;
photoFrame = CGRectMake(0, 0, 320, size.height*scaleFactor);
}
else
{
// Portrait
CGFloat scaleFactor = 320 / size.height;
photoFrame = CGRectMake(0, 0, size.width*scaleFactor, 320);
}
myPhotoImageView.frame = photoFrame;

Get UIImage from the CALayer attached to AVPlayer (extract frame from video playing)

I play a video with AVPlayer. It works ok.
Now I want to get a UIImage from the video playing (when I push a button for the moment).
Attached to my AVPlayer there is a CALayer that is used to display video on my UIView.
My idea is to get an UIImage from CALayer during video is playing.
I do this with code from another question:
UIImage from CALayer - iPhone SDK
However my UIImage is empty. The resolution is good but it is but fully white !!!
It seems that video doesn't write the contents of my CALayer.
Someone can help me?
Thanks
I couldn't get Meet's solution to work for me, but it got me thinking in the right direction.
Below is the code that I ended up using in my project. The method screenshotFromPlayer:maximumSize: accepts an instance of an AVPlayer from which to take a screenshot, and a CGSize that will be the returned image's maximum size.
- (UIImage *)screenshotFromPlayer:(AVPlayer *)player maximumSize:(CGSize)maxSize {
CMTime actualTime;
NSError *error;
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:player.currentItem.asset];
// Setting a maximum size is not necessary for this code to
// successfully get a screenshot, but it was useful for my project.
generator.maximumSize = maxSize;
CGImageRef cgIm = [generator copyCGImageAtTime:player.currentTime
actualTime:&actualTime
error:&error];
UIImage *image = [UIImage imageWithCGImage:cgIm];
CFRelease(cgIm);
if (nil != error) {
NSLog(#"Error making screenshot: %#", [error localizedDescription]);
NSLog(#"Actual screenshot time: %f Requested screenshot time: %f", CMTimeGetSeconds(actualTime),
CMTimeGetSeconds(self.recordPlayer.currentTime));
return nil;
}
return image;
}
Note also that one could use the method generateCGImagesAsynchronouslyForTimes:completionHandler: instead of copyCGImageAtTime:actualTime:error: (on the instance of AVAssetImageGenerator) to perform the image generation asynchronously.
This code sample generates a screenshot at the AVPlayer's currentTime, but any time could be used instead.
The code to get image from avplayer.
- (UIImage *)currentItemScreenShot
{
AVPlayer *abovePlayer = [abovePlayerView player];
CMTime time = [[abovePlayer currentItem] currentTime];
AVAsset *asset = [[[abovePlayerView player] currentItem] asset];
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
if ([imageGenerator respondsToSelector:#selector(setRequestedTimeToleranceBefore:)] && [imageGenerator respondsToSelector:#selector(setRequestedTimeToleranceAfter:)]) {
[imageGenerator setRequestedTimeToleranceBefore:kCMTimeZero];
[imageGenerator setRequestedTimeToleranceAfter:kCMTimeZero];
}
CGImageRef imgRef = [imageGenerator copyCGImageAtTime:time
actualTime:NULL
error:NULL];
if (imgRef == nil) {
if ([imageGenerator respondsToSelector:#selector(setRequestedTimeToleranceBefore:)] && [imageGenerator respondsToSelector:#selector(setRequestedTimeToleranceAfter:)]) {
[imageGenerator setRequestedTimeToleranceBefore:kCMTimePositiveInfinity];
[imageGenerator setRequestedTimeToleranceAfter:kCMTimePositiveInfinity];
}
imgRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
}
UIImage *image = [[UIImage alloc] initWithCGImage:imgRef];
CGImageRelease(imgRef);
[imageGenerator release];
return [image autorelease];
}
set [imageGenerator setRequestedTimeToleranceBefore:kCMTimeZero] and [imageGenerator setRequestedTimeToleranceAfter:kCMTimeZero] if you need precise time.
Try to get the image from the video file at specified instance using the AVAssetImageGenerator:
AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:[info objectForKey:#"UIImagePickerControllerReferenceURL"]] options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform=TRUE;
[asset release];
CMTime thumbTime = CMTimeMakeWithSeconds(0,30);
AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
if (result != AVAssetImageGeneratorSucceeded) {
}
UIImage *thumbImg = [[UIImage imageWithCGImage:im] retain];
[generator release];

Resources