Khi bạn chụp ảnh với UIImagePickerController trên iPhone, tôi và nhiều cư dân internet đã tìm thấy bạn cần chạy hình ảnh thông qua chức năng này để định hướng chúng đúng cách bạn đang ở đó).Tăng tốc hoặc loại bỏ chức năng chậm này trong ứng dụng máy ảnh iPhone
Có một sợi khổng lồ về nó ở đây: http://discussions.apple.com/message.jspa?messageID=7276709
Đáng tiếc, chức năng này có thể mất vài giây để chạy và nó thực sự là một phiền toái. Nếu bạn muốn sử dụng điều này và cho phép người dùng chụp ảnh nhanh, thậm chí bạn cần thêm NSLock hoặc bạn sẽ gặp phải một số sự cố.
Làm cách nào để tăng tốc hoặc loại bỏ mã này?
// orient photos and scale them to kMaxResolution
+ (UIImage*)scaleAndRotateImage:(UIImage *)image {
// we upload photos at a maximum resolution of 2048 x 1536
int kMaxResolution = 2048;
CGImageRef imgRef = image.CGImage;
CGFloat width = CGImageGetWidth(imgRef);
CGFloat height = CGImageGetHeight(imgRef);
CGAffineTransform transform = CGAffineTransformIdentity;
CGRect bounds = CGRectMake(0, 0, width, height);
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;
}
Tôi cũng đang sử dụng mã này. Chỉ cần tò mò, nhu cầu của NSLock sẽ được áp dụng ở đâu? Tôi không thấy bất kỳ sự cố nào khi sử dụng mã này. Làm thế nào để bạn có nghĩa là "cho phép người dùng chụp ảnh nhanh"? – Jasarien
Đối với những gì tôi có ý nghĩa về NSLock, hãy xem vài bình luận cuối cùng trên diễn đàn mà tôi đã liên kết. Bằng cách cho phép người dùng chụp ảnh nhanh, tôi có nghĩa là làm nền cho việc lưu ảnh. Bạn có chặn khi lưu ảnh vào đĩa không? Ngoài ra, ứng dụng của tôi ghi dữ liệu EXIF vào ảnh trong quá trình này, do đó, có thêm công việc đang được thực hiện để nhấn mạnh bộ nhớ. –
Tôi sử dụng một cái gì đó tương tự và nó rất, rất chậm. Có lẽ có một cách tốt hơn, rằng tôi không biết ... Câu hỏi hay. – Jordan