Così ho cercato di capire cosa sto facendo di sbagliato da un po 'ora e non riesco a capirlo. Quello che sto cercando di realizzare è questo:UIImmaginamento delle dimensioni non funzionante correttamente
- scattare una foto con
UIImagePickerController
- Prendete la foto risultante e ritagliare la parte superiore e inferiore in modo che diventi un quadrato (simile a Instagram)
- display che immagine all'interno di un
UIButton
Per qualche motivo, ogni volta che prende la foto finisce falsata nel UIButton
e sembra come se il ritaglio non funzionava correttamente. Ecco cosa faccio. All'interno del metodo didFinishPickingMediaWithInfo
Ho il seguente codice:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//Copy the image to the userImage variable
[picker dismissModalViewControllerAnimated:YES];
userImage = nil;
//Rotate & resize image
userImage = [self resizeAndRotatePhoto:(UIImage *)[info objectForKey:UIImagePickerControllerOriginalImage]];
NSLog(@"Userimage size, width: %f , height: %f", userImage.size.width , userImage.size.height);
//Update the image form field with the image and set the image in the controller
NSLog(@"Button size is height: %f , width: %f" , userImageAvatarButton.frame.size.height , userImageAvatarButton.frame.size.width);
[userImageAvatarButton.layer setMasksToBounds:YES];
[userImageAvatarButton.layer setCornerRadius:3.0];
[userImageAvatarButton setImage:userImage forState:UIControlStateNormal];
}
includerò il metodo resizeAndRotatePhoto momentaneamente, ma il risultato è al di sotto. Inoltre, nel codice sopra, @property (strong) (UIImage *)userImage;
è definito nel file di intestazione di ViewController. L'output del registro si traduce anche in:
2012-05-07 17:38:07.995 NewApp[10666:707] Userimage size, width: 1936.000000 , height: 1936.000000
2012-05-07 17:38:08.000 NewApp[10666:707] Button size is height: 60.000000 , width: 60.000000
Come si vede nell'immagine qui sotto, finisce distorto.
Per quanto riguarda il metodo di resizeAndRotate, qui è:
- (UIImage *)resizeAndRotatePhoto:(UIImage *)source
{
if(source.imageOrientation == UIImageOrientationRight)
{
source = [self rotateImage:source byDegrees:90];
}
if(userImage.imageOrientation == UIImageOrientationLeft)
{
source = [self rotateImage:source byDegrees:-90];
}
CGFloat x,y;
CGFloat size;
if(source.size.width > source.size.height){
size = source.size.height;
x = (source.size.width - source.size.height)/2;
y = 0;
}
else {
size = source.size.width;
x = 0;
y = (source.size.height - source.size.width)/2;
}
CGImageRef imageRef = CGImageCreateWithImageInRect([source CGImage], CGRectMake(x,y,size,size));
return [UIImage imageWithCGImage:imageRef];
}
A questo punto non ho idea di come ottenere questa immagine di presentarsi senza distorsioni. Sembra che stia sbagliando e mostrandolo sbagliato nonostante il sistema abbia affermato che l'immagine è effettivamente un quadrato.
GRAZIE. Dopo ore e ore di frustrazione e ricerca online cercando vicino a 20 soluzioni proposte, questa è l'UNICA soluzione che ha funzionato. Molte grazie. – thephatp
sì. mi ha risparmiato ore di lavoro pure. Apprezzo la categoria! – kevinl