简体   繁体   English

检索前优化电话簿联系人图像

[英]Optimize phonebook contact image before retrieving

I have used this piece of code for getting a persons contact image from iphone phonebook 我已使用这段代码从iPhone电话簿中获取人的联系方式


CFDataRef imgData = ABPersonCopyImageData(person);
UIImage *img = [UIImage imageWithData:(NSData *)imgData];
CFRelease(imgData);

Some users add a contact image that is more than 2 or 3 MB in size. 某些用户添加的联系人图像大小超过2或3 MB。 what i want to do is to get an optimized image, like 250 x 250 resolution so that i dont get any memory issues which i'm facing now. 我要做的是获取优化的图像,例如250 x 250分辨率,这样我就不会遇到我现在面临的任何内存问题。

I managed to find this piece of code on http://blog.logichigh.com/2008/06/05/uiimage-fix/ It does the job. 我设法在http://blog.logichigh.com/2008/06/05/uiimage-fix/上找到了这段代码。 But when scaling an image more than 3.5MB on an iPhone 3G, the app crashes. 但是,当在iPhone 3G上缩放图像超过3.5MB时,该应用程序将崩溃。 So i was hoping if there is some code that would do this job better (not that the one below is not good) or if there is a way to get an optimized contact image, like some api. 所以我希望是否有一些代码可以更好地完成这项工作(不是下面的代码不是很好),或者是否有办法获得优化的联系方式,例如一些api。

Thank you for reading. 感谢您的阅读。


UIImage *scaleAndRotateImage(UIImage *image)
{
    int kMaxResolution = 320; // Or whatever
    CGImageRef imgRef = image.CGImage;
    CGFloat width = CGImageGetWidth(imgRef);
    CGFloat height = CGImageGetHeight(imgRef);
    CGAffineTransform transform = CGAffineTransformIdentity;
    CGRect bounds = CGRectMake(0, 0, width, height);enter code here
    if (width > kMaxResolution || height > kMaxResolution) {
        CGFloat ratio = width/height;
        if (ratio > 1) {
            bounds.size.width = kMaxResolution;
            bounds.size.height = bounds.size.width / ratio;
        }
        else {
            bounds.size.height = kMaxResolution;
            bounds.size.width = bounds.size.height * ratio;
        }
    }
    CGFloat scaleRatio = bounds.size.width / width;
    CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
    CGFloat boundHeight;
    UIImageOrientation orient = image.imageOrientation;
    switch(orient) {
        case UIImageOrientationUp: //EXIF = 1
            transform = CGAffineTransformIdentity;
            break;
        case UIImageOrientationUpMirrored: //EXIF = 2
            transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
            transform = CGAffineTransformScale(transform, -1.0, 1.0);
            break;
        case UIImageOrientationDown: //EXIF = 3
            transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;
        case UIImageOrientationDownMirrored: //EXIF = 4
            transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
            transform = CGAffineTransformScale(transform, 1.0, -1.0);
            break;
        case UIImageOrientationLeftMirrored: //EXIF = 5
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
            transform = CGAffineTransformScale(transform, -1.0, 1.0);
            transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
            break;
        case UIImageOrientationLeft: //EXIF = 6
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
            transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
            break;
        case UIImageOrientationRightMirrored: //EXIF = 7
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeScale(-1.0, 1.0);
            transform = CGAffineTransformRotate(transform, M_PI / 2.0);
            break;
        case UIImageOrientationRight: //EXIF = 8
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);
            transform = CGAffineTransformRotate(transform, M_PI / 2.0);
            break;
        default:
            [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];
    }
    UIGraphicsBeginImageContext(bounds.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) {
        CGContextScaleCTM(context, -scaleRatio, scaleRatio);
        CGContextTranslateCTM(context, -height, 0);
    }
    else {
        CGContextScaleCTM(context, scaleRatio, -scaleRatio);
        CGContextTranslateCTM(context, 0, -height);
    }
    CGContextConcatCTM(context, transform);
    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
    UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return imageCopy;
}

Found the solution. 找到了解决方案。 May be for someone like me who dint know about this. 可能适合像我这样的人。 While working with ios4.x you can use ABPersonCopyImageDataWithFormat(recordRef,kABPersonImageFormatThumbnail) to get the thumbnail version of the actual image. 在使用ios4.x时,您可以使用ABPersonCopyImageDataWithFormat(recordRef,kABPersonImageFormatThumbnail)获取实际图像的缩略图版本。

Take a good look at Trevor Howard's great post about resizing UIImage s. 好好看看Trevor Howard关于调整UIImage大小的精彩文章。

http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/ http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

Hope it helps. 希望能帮助到你。

OR , you can implement this class method: ,您可以实现此类方法:

+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();
    return newImage;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM