I want to pick an image and then crop to a circle. Cropping to a square is easy enough. I put:
Code:
self.imagePicker.allowsEditing = YES;
under viewDidLoad.
and then
Code:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
if (picker == imagePicker) {
// set your obverse image
// Delete any existing image.
NSManagedObject *oldImage = recipe.image;
if (oldImage != nil) {
[recipe.managedObjectContext deleteObject:oldImage];
}
// Create an image object for the new image.
NSManagedObject *obverse = [NSEntityDescription insertNewObjectForEntityForName:@"Image" inManagedObjectContext:recipe.managedObjectContext];
recipe.image = obverse;
// Set the image for the image managed object.
//UIImage *selectedImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
UIImage *selectedImage = [info objectForKey:@"UIImagePickerControllerEditedImage"];
[recipe.image setValue:selectedImage forKey:@"obverse"];
// Create a thumbnail version of the image for the recipe object.
CGSize size = selectedImage.size;
CGFloat ratio = 0;
if (size.width > size.height) {
ratio = 320.0 / size.width;
} else {
ratio = 320.0 / size.height;
}
CGRect rect = CGRectMake(0.0, 0.0, ratio * size.width, ratio * size.height);
UIGraphicsBeginImageContext(rect.size);
[selectedImage drawInRect:rect];
recipe.thumbnailImage = UIGraphicsGetImageFromCurrentImageContext();
}
This allows me to crop my image to a square that is 320x320.
Anyone know how to change it so that the image is cropped to a circle that is 320 wide?
You can create a circle (or just any arbitrary shape/pattern) masking graphic in some graphics editor and add it to your project, and then use CGImageMaskCreate (to create your mask image), and CGImageCreateWithMask (to apply your mask over your photo).
I do this with a sort of torn-off looking mask to create a thumbnail image of photos for table cells in my app.
__________________ Recall It!Tag your notes. Tag your photos. Tag your thoughts. Tag your life.
You can create a circle (or just any arbitrary shape/pattern) masking graphic in some graphics editor and add it to your project, and then use CGImageMaskCreate (to create your mask image), and CGImageCreateWithMask (to apply your mask over your photo).
I do this with a sort of torn-off looking mask to create a thumbnail image of photos for table cells in my app.
Wow, I had no idea it was that easy. Thanks for the tip. A simpe search turned up this how-to
That sample code you linked to looks like a good overview, but make sure you release your CGImageRefs returned by CGImageMaskCreate and CGImageCreateWithMask (which it doesn't do in the sample code)!
That sample code you linked to looks like a good overview, but make sure you release your CGImageRefs returned by CGImageMaskCreate and CGImageCreateWithMask (which it doesn't do in the sample code)!
But how would you then go on to make the Mask transparent (and also the thing it was masking)?
In the areas where your mask image is black, your original image will show through... in areas where the mask image is white, your original image won't show through.
__________________ Recall It!Tag your notes. Tag your photos. Tag your thoughts. Tag your life.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
// Set the image for the image managed object.
//UIImage *selectedImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
UIImage *selectedImage = [info objectForKey:@"UIImagePickerControllerEditedImage"];
[recipe.image setValue:selectedImage forKey:@"obverse"];
// Create a thumbnail version of the image for the recipe object.
CGSize size = selectedImage.size;
CGFloat ratio = 0;
if (size.width > size.height) {
ratio = 320.0 / size.width;
} else {
ratio = 320.0 / size.height;
}
CGRect rect = CGRectMake(0.0, 0.0, ratio * size.width, ratio * size.height);
UIGraphicsBeginImageContext(rect.size);
[selectedImage drawInRect:rect];
recipe.thumbnailImage = UIGraphicsGetImageFromCurrentImageContext();
}
But from what I've heard, it seems that if the mask was white, it would be as is, so you would have a circle, which then had the rest of a white rectangle.
But from what I've heard, it seems that if the mask was white, it would be as is, so you would have a circle, which then had the rest of a white rectangle.
No, as was explain earlier, the black portions of the mask will show, the white portions will be transparent.
Hmm... what exactly is not working, and in what way?
Well, I have:
Code:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
// Set the image for the image managed object.
//UIImage *selectedImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
UIImage *selectedImage = [info objectForKey:@"UIImagePickerControllerEditedImage"];
[recipe.image setValue:selectedImage forKey:@"obverse"];
// Create a thumbnail version of the image for the recipe object.
CGSize size = selectedImage.size;
CGFloat ratio = 0;
if (size.width > size.height) {
ratio = 320.0 / size.width;
} else {
ratio = 320.0 / size.height;
}
CGRect rect = CGRectMake(0.0, 0.0, ratio * size.width, ratio * size.height);
UIGraphicsBeginImageContext(rect.size);
[selectedImage drawInRect:rect];
recipe.thumbnailImage = UIGraphicsGetImageFromCurrentImageContext();
}
I'm sorry for my stupidity. I am a student and have to work at the moment. My task is to build an iphone app, which can choose or take a picture and send this to a webserver. This is what I've already managed to do.
Now the second task is, that you can edit the picture. Just like Krye wanted to do.
I took dljeffery code. but i have absolutely no idea how to integrate this code into my program.
I have one ViewController in which I can choose or take the picture. now i want to set a button "edit picture", which should open another view where i can crop the picture now.. But I dont really know, how to to get the picture from the one class to the other.
plz can somebody help me out, otherwise my boss is driving crazy
if you want to have the code for choosing, taking pictures and that to a webserver, just email me.. i copied and pasted it from other users and tutorial..