I am trying to create my first App for iOS. I would like to create an App which can load a Photo from Library or create a new one with the camera. Then after when I created a photo or load a picture from my library, i would like that it will go to a filter, for example sepia (like it is used in every tutorial) and that I can change the value right now with a slicer on the screen. I will put my source code here, because I dont know if I am doing it right. This is everything from my ViewController.m file
What should I do now to get it working?
#import "ViewController.h"
#interface ViewController ()
{
CIContext *context;
CIFilter *filter;
CIImage *beginImage;
}
#property (nonatomic, retain) UIDocumentInteractionController *documentController;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"image" ofType:#"png"];
NSURL *fileNamePath = [NSURL fileURLWithPath:filePath];
CIImage *inputImage = [CIImage imageWithContentsOfURL:fileNamePath];
CIFilter *filter = [CIFilter filterWithName:#"CISepia" keysAndValues:kCIInputImageKey, inputImage, #"inputIntensity", #0.8, nil];
CIImage *outputImage = [filter outputImage];
self.ImageView.image = [UIImage imageWithCIImage:outputImage];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)TakePhoto:(id)sender {
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
[picker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:picker animated:YES completion:NULL];
}
- (IBAction)ChoosePhoto:(id)sender {
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
[picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:picker animated:YES completion:NULL];
}
-(void)imagePickerController:(nonnull UIImagePickerController *)picker didFinishPickingMediaWithInfo:(nonnull NSDictionary<NSString *,id> *)info {
image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
[self.ImageView setImage:image];
[self dismissViewControllerAnimated:YES completion:NULL];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (IBAction)amountSliderValueChanged:(id)sender
{
float slideValue = _Slider.value;
[filter setValue:#(slideValue)
forKey:#"inputIntensity"];
CIImage *outputImage = [filter outputImage];
CGImageRef cgimg = [context createCGImage:outputImage
fromRect:[outputImage extent]];
UIImage *newImage = [UIImage imageWithCGImage:cgimg];
self.ImageView.image = newImage;
CGImageRelease(cgimg);
}
Related
I am working to create a view in my app that will allow you to take a picture, and then will automatically draw a frame around that image. The issue I am facing is that there is some blank space around the top and sides when I do this. Here is my code:
UPDATE: If I run this in a 6S, you get image shown below. If in 6S Plus, you get the final image.
-(void)viewDidLoad {
UIBarButtonItem *takePhoto = [[UIBarButtonItem alloc] initWithTitle:#"Take Picture" style:UIBarButtonItemStylePlain target:self action:#selector(takingPhoto)];
UIBarButtonItem *savePhoto = [[UIBarButtonItem alloc] initWithTitle:#"Save" style:UIBarButtonItemStylePlain target:self action:#selector(saveIt)];
self.navigationItem.rightBarButtonItems = #[takePhoto, savePhoto];
}
- (void)takingPhoto{
ipc = [[UIImagePickerController alloc] init];
ipc.delegate = self;
ipc.allowsEditing = YES;
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:ipc animated:YES completion:NULL];
}
else
{
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:ipc animated:YES completion:NULL]; }
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
self.imgView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
[self applyFilter];
[picker dismissViewControllerAnimated:YES completion:nil];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)applyFilter {
NSLog(#"Running");
UIImage *borderImage = [UIImage imageNamed:#"IMG_8055.PNG"];
NSData *dataFromImage = UIImageJPEGRepresentation(self.imgView.image, 1);
CIImage *beginImage= [CIImage imageWithData:dataFromImage];
CIContext *context = [CIContext contextWithOptions:nil];
CIImage *border =[CIImage imageWithData:UIImagePNGRepresentation(borderImage)];
CIFilter *filter= [CIFilter filterWithName:#"CISourceOverCompositing"]; //#"CISoftLightBlendMode"];
[filter setDefaults];
[filter setValue:border forKey:#"inputImage"];
[filter setValue:beginImage forKey:#"inputBackgroundImage"];
CIImage *outputImage = [filter valueForKey:#"outputImage"];
CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];
UIImage *newImg = [UIImage imageWithCGImage:cgimg];
self.imgView.image = newImg;
}
In the simulator, it looks like this:
However, when I look in saved photos after I have saved it, it looks like this:
Why does it have the white space on top and right side?
Here is the aforementioned 6S Plus image:
I need to load a image from a URL into a UIImageView. Each time the URL would be different because i am JSON parsing a website. I am able to get the URL. However when i run the app, there is no image in the UIImageView.
This is my code
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
if ([eventNameDesc containsString:#"src="]) {
eventNameDesc = #"";
}
else {
eventDescription.text = eventNameDesc;
}
self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(shareButtonPressed)];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)shareButtonPressed {
NSString *shareText = eventNameDesc;
NSArray *itemsToShare = #[shareText];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
activityVC.excludedActivityTypes = #[];
[self presentViewController:activityVC animated:YES completion:nil];
}
- (void)loadImage {
NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:self.imageURL]];
UIImage* image = [[UIImage alloc] initWithData:imageData];
[self performSelectorOnMainThread:#selector(displayImage:) withObject:image waitUntilDone:NO];
}
- (void)displayImage:(UIImage *)image {
[self.ImageView setImage:image]; //UIImageView
}
#end
What is wrong with my code? Somebody please help.
You can make a UIImage object using the code provided here
After that you can add your UIImage object to any UIImageView object.
This is the code that works for me.
UIImage *anImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://4.bp.blogspot.com/-K7vl-ShXrNc/VQU-D0NTFgI/AAAAAAAAAOg/aBIUOwF2nEQ/s1600/contact.png"]]];
UIImageView *temp = [[UIImageView alloc] initWithImage:anImage];
[self.view addSubview:temp];
Here is a screenshot.
Trying to load a camera/album photo into a 'add border' loop, but can't get it to work.
The app basically takes a photo or picks one from album, then allows the user to choose between 4 different borders but as the user switches borders it doesn't revert back to the original photo it just layers the borders on top.
I've tried using
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
But it doesn't like 'info'
Code below.
// ABViewController.m
#import "ABViewController.h"
#import <QuartzCore/QuartzCore.h>
#interface ABViewController ()
#property (nonatomic) IBOutlet UIImageView *imageView;
#property (nonatomic) IBOutlet UIButton *border1;
#property (nonatomic) IBOutlet UIButton *border2;
#property (nonatomic) IBOutlet UIButton *border3;
#property (nonatomic) IBOutlet UIButton *border4;
#end
#implementation ABViewController
#synthesize imageView;
#synthesize border1, border2, border3, border4;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
border1.tag =1;
border2.tag =2;
border3.tag =3;
border4.tag =4;
imageView.image = [UIImage imageNamed:#"a7qIB4OiXsz5Q0BG4ipZV0FLF5_jB71p24h7Oa2ExYU.jpg"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//Border code
- (IBAction)borderButtonTapped:(UIButton *)sender {
UIImage* image=(UIImage*)[#"UIImagePickerControllerOriginalImage"];
UIImage *borderImage = [UIImage imageNamed:[NSString stringWithFormat:#"borderImage%i.png", sender.tag]];
NSData *dataFromImage = UIImageJPEGRepresentation(imageView.image, 1);
CIImage *beginImage= [CIImage imageWithData:dataFromImage];
CIContext *context = [CIContext contextWithOptions:nil];
CIImage *border =[CIImage imageWithData:UIImagePNGRepresentation(borderImage)];
CIFilter *filter= [CIFilter filterWithName:#"CISourceOverCompositing"]; //#"CISoftLightBlendMode"];
[filter setDefaults];
[filter setValue:border forKey:#"inputImage"];
[filter setValue:beginImage forKey:#"inputBackgroundImage"];
CIImage *outputImage = [filter valueForKey:#"outputImage"];
CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];
UIImage *newImg = [UIImage imageWithCGImage:cgimg];
imageView.image = newImg;
}
// Camera CODE
- (IBAction)TakePhoto {
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
[picker setSourceType:UIImagePickerControllerSourceTypeCamera];
[picker setShowsCameraControls:YES];
[picker setAllowsEditing:YES];
[self presentViewController:picker animated:YES completion:NULL];
[picker release];
}
- (IBAction)ChooseExisting {
picker2 = [[UIImagePickerController alloc] init];
picker2.delegate = self;
[picker2 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[picker2 setAllowsEditing:YES];
[self presentViewController:picker2 animated:YES completion:NULL];
[picker2 release];
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
image = [info objectForKey:UIImagePickerControllerEditedImage];
[imageView setImage:image];
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:NULL];
}
#end
Try the following...
Create a NSDictionary reference to hold onto the info:
#implementation ABViewController{
NSDictionary *imageInfo;
}
In the imagePickerController: didFinishPickingMediaWithInfo: method add:
imageInfo = info;
In the borderButtonTapped method, replace:
UIImage* image=(UIImage*)[#"UIImagePickerControllerOriginalImage"];
with:
UIImage *image = [imageInfo valueForKey:UIImagePickerControllerOriginalImage];
Edit:
and change:
NSData *dataFromImage = UIImageJPEGRepresentation(imageView.image, 1);
to:
NSData *dataFromImage = UIImageJPEGRepresentation(image, 1);
Iam trying to capture video using front camera in ios and convert that into sequence of images I am trying to use avassetimagegenerator for that . Iam new to AV Foundation please help me in this , thanks .
First present an UIImagePickerController
- (void)presentVideoPicker {
UIImagePickerController *anImagePicker = [[UIImagePickerController alloc] init];
anImagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
anImagePicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
anImagePicker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *)kUTTypeMovie, nil];
anImagePicker.allowsEditing = YES;
anImagePicker.delegate = self;
UIPopoverController *aPopoverController = [[[UIPopoverController alloc] initWithContentViewController:anImagePicker] autorelease];
[aPopoverController presentPopoverFromRect:self.view.frame inView:[[self view] window]
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
Capture the Video in UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *aMediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([aMediaType isEqualToString:(NSString *)kUTTypeMovie]) {
NSURL *aVideoURL = [info objectForKey:UIImagePickerControllerMediaURL];
[self readMovie:aVideoURL];
// If you want the Duration fo the Video, Use this
AVPlayerItem *aPlayerItem = [AVPlayerItem playerItemWithURL:aVideoURL];
CMTime aDuration = aPlayerItem.duration;
float anInSeconds = CMTimeGetSeconds(aDuration);
NSInteger aDuration = nearbyintf(anInSeconds);
}
}
Now code for the Method you called in the Delegate - (void) readMovie:(NSURL *)url
Refer this link:
http://www.7twenty7.com/blog/2010/11/video-processing-with-av-foundation
...
Once you generate the CVImageBuffer for an Image,
Refer
how to convert a CVImageBufferRef to UIImage
to convert it to UIImage
i'm trying to manage image rotation in imagePickerController:didFinishPickingMediaWithInfo, but after several attempts, I did not find any solutions. My last try is:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
// load the storyboard by name
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:[ApplicationSupport getStoryBoardName] bundle:nil];
UploadViewController *uploadView = [storyboard instantiateViewControllerWithIdentifier:#"ViewUploadFile"];
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
NSDictionary *metadataImage = [info valueForKey:UIImagePickerControllerMediaMetadata];
uploadView.imageToUpload = image;
if([[metadataImage objectForKey:#"Orientation"] intValue] == 3) {
UIImage *imageRotated = [UIImage imageWithCGImage:[image CGImage] scale:[image scale] orientation:UIImageOrientationDown];
uploadView.dataToUpload = UIImagePNGRepresentation(imageRotated);
} else {
uploadView.dataToUpload = UIImagePNGRepresentation(image);
}
// ETC...
}
I can't and I don't want to use JPEGRepresentation. I need to rotate my photo by 180° when iPad is in specific position.
Any idea?
have a Look at this: http://www.catamount.com/blog/1015/uiimage-extensions-for-cutting-scaling-and-rotating-uiimages/
Just call
UIImage *rotatedImage = [originalImage imageRotatedByDegrees:180.0];
Source
UploadView.DataToUpload = [UIImage imageWithCGImage:[image CGImage] scale:1.0 orientation : UIImageOrientationDown];
For 180 deg rotation.