When taking pictures from a photo album or camera as an avatar, the solution to the black problem occasionally occurs, as well as the solution to the Avatar rotation and other problems, the Avatar Solution

Source: Internet
Author: User

When taking pictures from a photo album or camera as an avatar, the solution to the black problem occasionally occurs, as well as the solution to the Avatar rotation and other problems, the Avatar Solution

I tried many methods on the Internet and did not solve it! The purpose is the same. It is to rotate the orientation of the image to achieve the desired effect !!

Let's look at the code. I used imagePickerController to get the Avatar.

# Pragma mark-UIImagePickerControllerDelegate methods // enter here after selecting an image-(void) imagePickerController :( UIImagePickerController *) picker didFinishPickingMediaWithInfo :( NSDictionary *) info {// process the photo here // NSString * type = [info objectForKey: UIImagePickerControllerMediaType]; // * UIImage * originalImage = [info objectForKey: UIImagePickerControllerOriginalImage]; UIImageOrientation orientation = originalImage. imageOrientation; if (orientation = UIImageOrientationUp) {[self dismissViewControllerAnimated: YES completion: nil]; _ memberIconView. image = originalImage;} else if (orientation = UIImageOrientationRight) {[self dismissViewControllerAnimated: YES completion: nil]; _ memberIconView. image = [originalImage imageRotatedByDegrees: 90.0] ;}}

Here I want to talk about it. On http://stackoverflow.com/, some people have pointed out that this question can be used to solve the problem, but my question still exists! The method on stackoverflow is as follows:

-(UIImage *)fixOrientation{    if (self.imageOrientation == UIImageOrientationUp) {        return self;    }    CGAffineTransform transform;    NSLog(@"%i",self.imageOrientation);    switch (self.imageOrientation) {        case UIImageOrientationDown:        case UIImageOrientationDownMirrored:        {            transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height);            transform = CGAffineTransformRotate(transform, M_PI);            break;        }        case UIImageOrientationLeft:        case UIImageOrientationLeftMirrored:        {            transform = CGAffineTransformTranslate(transform, self.size.width, 0);            transform = CGAffineTransformRotate(transform, M_PI_2);            break;        }        case UIImageOrientationRight:        {            transform = CGAffineTransformTranslate(transform, 0, self.size.height);            transform = CGAffineTransformRotate(transform, -M_PI_2);            break;        }        case UIImageOrientationRightMirrored:        {            transform = CGAffineTransformTranslate(transform, 0, self.size.height);            transform = CGAffineTransformRotate(transform, -M_PI_2);            break;        }        default:            break;    }    switch (self.imageOrientation) {        case UIImageOrientationUpMirrored:        case UIImageOrientationDownMirrored:            transform = CGAffineTransformTranslate(transform, self.size.width, 0);            transform = CGAffineTransformScale(transform, -1, 1);            break;                    case UIImageOrientationLeftMirrored:        case UIImageOrientationRightMirrored:            transform = CGAffineTransformTranslate(transform, self.size.height, 0);            transform = CGAffineTransformScale(transform, -1, 1);            break;    }        CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height,                                             CGImageGetBitsPerComponent(self.CGImage), 0,                                             CGImageGetColorSpace(self.CGImage),                                             CGImageGetBitmapInfo(self.CGImage));    CGContextConcatCTM(ctx, transform);    switch (self.imageOrientation) {        case UIImageOrientationLeft:        case UIImageOrientationLeftMirrored:        case UIImageOrientationRight:        case UIImageOrientationRightMirrored:            // Grr...            CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage);            break;                    default:            CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage);            break;    }        // And now we just create a new UIImage from the drawing context    CGImageRef cgimg = CGBitmapContextCreateImage(ctx);    UIImage *img = [UIImage imageWithCGImage:cgimg];    CGContextRelease(ctx);    CGImageRelease(cgimg);    return img;}

Finally, I combined other methods to populate the class extension of UIImage to solve the problem:

Method: In fact, he also rotates the angle according to the direction, but it works.

- (UIImage *)imageRotatedByRadians:(CGFloat)radians {    return [self imageRotatedByDegrees:radiansToDegrees(radians)];}- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees {    // calculate the size of the rotated view's containing box for our drawing space    UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.size.width, self.size.height)];    CGAffineTransform t = CGAffineTransformMakeRotation(degreesToRadians(degrees));    rotatedViewBox.transform = t;    CGSize rotatedSize = rotatedViewBox.frame.size;    // Create the bitmap context    UIGraphicsBeginImageContext(rotatedSize);    CGContextRef bitmap = UIGraphicsGetCurrentContext();    // Move the origin to the middle of the image so we will rotate and scale around the center.    CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);    // Rotate the image context    CGContextRotateCTM(bitmap, degreesToRadians(degrees));    // Now, draw the rotated/scaled image into the context    CGContextScaleCTM(bitmap, 1.0, -1.0);    CGContextDrawImage(bitmap, CGRectMake(-self.size.width / 2, -self.size.height / 2, self.size.width, self.size.height), [self CGImage]);    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();    return newImage;}

Finally, add all the source code for this class extension, and paste and copy it directly:

First header file: UIImage + fixOrientation. h

#import <UIKit/UIKit.h>@interface UIImage (fixOrientation)- (UIImage *)fixOrientation;- (UIImage *)imageRotatedByRadians:(CGFloat)radians;- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees;@end

Next, run the. m file: UIImage + fixOrientation. m.

#import "UIImage+fixOrientation.h"CGFloat degreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};CGFloat radiansToDegrees(CGFloat radians) {return radians * 180/M_PI;};@implementation UIImage (fixOrientation)-(UIImage *)fixOrientation{    if (self.imageOrientation == UIImageOrientationUp) {        return self;    }    CGAffineTransform transform;    NSLog(@"%i",self.imageOrientation);    switch (self.imageOrientation) {        case UIImageOrientationDown:        case UIImageOrientationDownMirrored:        {            transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height);            transform = CGAffineTransformRotate(transform, M_PI);            break;        }        case UIImageOrientationLeft:        case UIImageOrientationLeftMirrored:        {            transform = CGAffineTransformTranslate(transform, self.size.width, 0);            transform = CGAffineTransformRotate(transform, M_PI_2);            break;        }        case UIImageOrientationRight:        {            transform = CGAffineTransformTranslate(transform, 0, self.size.height);            transform = CGAffineTransformRotate(transform, -M_PI_2);            break;        }        case UIImageOrientationRightMirrored:        {            transform = CGAffineTransformTranslate(transform, 0, self.size.height);            transform = CGAffineTransformRotate(transform, -M_PI_2);            break;        }        default:            break;    }    switch (self.imageOrientation) {        case UIImageOrientationUpMirrored:        case UIImageOrientationDownMirrored:            transform = CGAffineTransformTranslate(transform, self.size.width, 0);            transform = CGAffineTransformScale(transform, -1, 1);            break;                    case UIImageOrientationLeftMirrored:        case UIImageOrientationRightMirrored:            transform = CGAffineTransformTranslate(transform, self.size.height, 0);            transform = CGAffineTransformScale(transform, -1, 1);            break;    }        CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height,                                             CGImageGetBitsPerComponent(self.CGImage), 0,                                             CGImageGetColorSpace(self.CGImage),                                             CGImageGetBitmapInfo(self.CGImage));    CGContextConcatCTM(ctx, transform);    switch (self.imageOrientation) {        case UIImageOrientationLeft:        case UIImageOrientationLeftMirrored:        case UIImageOrientationRight:        case UIImageOrientationRightMirrored:            // Grr...            CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage);            break;                    default:            CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage);            break;    }        // And now we just create a new UIImage from the drawing context    CGImageRef cgimg = CGBitmapContextCreateImage(ctx);    UIImage *img = [UIImage imageWithCGImage:cgimg];    CGContextRelease(ctx);    CGImageRelease(cgimg);    return img;}- (UIImage *)imageRotatedByRadians:(CGFloat)radians {    return [self imageRotatedByDegrees:radiansToDegrees(radians)];}- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees {    // calculate the size of the rotated view's containing box for our drawing space    UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.size.width, self.size.height)];    CGAffineTransform t = CGAffineTransformMakeRotation(degreesToRadians(degrees));    rotatedViewBox.transform = t;    CGSize rotatedSize = rotatedViewBox.frame.size;    // Create the bitmap context    UIGraphicsBeginImageContext(rotatedSize);    CGContextRef bitmap = UIGraphicsGetCurrentContext();    // Move the origin to the middle of the image so we will rotate and scale around the center.    CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);    // Rotate the image context    CGContextRotateCTM(bitmap, degreesToRadians(degrees));    // Now, draw the rotated/scaled image into the context    CGContextScaleCTM(bitmap, 1.0, -1.0);    CGContextDrawImage(bitmap, CGRectMake(-self.size.width / 2, -self.size.height / 2, self.size.width, self.size.height), [self CGImage]);    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();    return newImage;}@end



When a qq video is sent, the other party has a camera, but I can see that the other party's profile picture is in black screen. What is the reason? The other party can watch videos with others.

Sometimes the network speed is unstable.
I met my friend SP before.
Switch back and forth.
After hanging up, you can send it again or send it again.

Why do users sometimes have a camera icon when they point their mouse to a QQ Avatar when they are not online?

I am very clear about this !!! When he is not online, when you point his mouse to his QQ profile picture, there is a camera icon, because the last time he went online, there was a camera, so when he is not online, you will also see a camera. If he does not have a camera when he goes online next time, then you will not see it again.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.