通过函数调整图像大小时出现随机线?

发布于 2024-11-08 11:05:18 字数 3612 浏览 3 评论 0原文

用户选择图像后,它会保存到手机中并像这样存储:

// When a user selects a image
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
[picker.parentViewController dismissModalViewControllerAnimated:NO];

int orient = image.imageOrientation;
NSString *theOrientation = [NSString stringWithFormat: @"%d", orient];
NSString *latestIDQuery = @"";
NSArray *results = [database executeQuery:@"SELECT * FROM images WHERE status!='uploaded' ORDER BY identifier DESC LIMIT 0,1"]; 
for (NSDictionary *row in results) {
    latestIDQuery = [row objectForKey:@"id"];
}

int latestID = [latestIDQuery intValue];
int newID = latestID + 1;

NSString *newIDString = [[NSString alloc] initWithFormat:@"%d", newID];
NSString *imageURL = [NSString stringWithFormat:@"Documents/%@_process.jpg",newIDString];

// Upload image and insert into database
NSString *uploadImagePath = [NSString stringWithFormat:@"%@_process.jpg",newIDString];
NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:imageURL];

float tooBig = 800.0;
UIImage *tempImage = [self scaleImage:image:tooBig:tooBig];

[UIImageJPEGRepresentation(tempImage, 1.0) writeToFile:jpgPath atomically:NO];
NSString *theID = [NSString stringWithFormat:@"%d", newID];

[database executeNonQuery:@"INSERT INTO images (id, thumbnail, album, status, orient, ready) VALUES (?, ?, ?, 'uploading', ?, 'no');", theID, uploadImagePath, selectedID, theOrientation];

NSString *onestatus;
NSArray *getAlbumID = [database executeQuery:@"SELECT * FROM images"];
for (NSDictionary *getAlbumIDRow in getAlbumID) {
    NSString *oneid = [getAlbumIDRow objectForKey:@"id"];
    NSString *onethumb = [getAlbumIDRow objectForKey:@"thumbnail"];
    NSString *onestatus = [getAlbumIDRow objectForKey:@"status"];
}

TableViewAppDelegate *dataCeter = (TableViewAppDelegate *)[[UIApplication sharedApplication] delegate];
dataCeter.dataSix = nil;
NSString *databaseURL = [NSString stringWithFormat:@"%@_process.jpg",newIDString];
dataCeter.dataSix = databaseURL;

[newIDString release];
[self presentModalViewController:captionView animated:NO];  
}

但是,它会在随机一侧保存白色边框。下面是它的外观示例(添加了黑色背景以使其更加明显)。

我已将其范围缩小到我的调整大小代码:

// How to scale the image
- (UIImage *)scaleImage:(UIImage *) image: (float)maxWidth:(float) maxHeight {

CGImageRef imgRef = image.CGImage;

CGFloat width = CGImageGetWidth(imgRef);
CGFloat height = CGImageGetHeight(imgRef);

if (width <= maxWidth && height <= maxHeight)
{
    return image;
}

CGAffineTransform transform = CGAffineTransformIdentity;
CGRect bounds = CGRectMake(0, 0, width, height);

if (width > maxWidth || height > maxHeight)
{
    CGFloat ratio = width/height;
    if (ratio > 1)
    {
        bounds.size.width = maxWidth;
        bounds.size.height = bounds.size.width / ratio;
    }
    else
    {
        bounds.size.height = maxHeight;
        bounds.size.width = bounds.size.height * ratio;
    }
}

CGFloat scaleRatio = bounds.size.width / width;
UIGraphicsBeginImageContext(bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
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;
}

请帮忙,谢谢。

白线

After a user picks a image, it gets saved to their phone and stored like this:

// When a user selects a image
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
[picker.parentViewController dismissModalViewControllerAnimated:NO];

int orient = image.imageOrientation;
NSString *theOrientation = [NSString stringWithFormat: @"%d", orient];
NSString *latestIDQuery = @"";
NSArray *results = [database executeQuery:@"SELECT * FROM images WHERE status!='uploaded' ORDER BY identifier DESC LIMIT 0,1"]; 
for (NSDictionary *row in results) {
    latestIDQuery = [row objectForKey:@"id"];
}

int latestID = [latestIDQuery intValue];
int newID = latestID + 1;

NSString *newIDString = [[NSString alloc] initWithFormat:@"%d", newID];
NSString *imageURL = [NSString stringWithFormat:@"Documents/%@_process.jpg",newIDString];

// Upload image and insert into database
NSString *uploadImagePath = [NSString stringWithFormat:@"%@_process.jpg",newIDString];
NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:imageURL];

float tooBig = 800.0;
UIImage *tempImage = [self scaleImage:image:tooBig:tooBig];

[UIImageJPEGRepresentation(tempImage, 1.0) writeToFile:jpgPath atomically:NO];
NSString *theID = [NSString stringWithFormat:@"%d", newID];

[database executeNonQuery:@"INSERT INTO images (id, thumbnail, album, status, orient, ready) VALUES (?, ?, ?, 'uploading', ?, 'no');", theID, uploadImagePath, selectedID, theOrientation];

NSString *onestatus;
NSArray *getAlbumID = [database executeQuery:@"SELECT * FROM images"];
for (NSDictionary *getAlbumIDRow in getAlbumID) {
    NSString *oneid = [getAlbumIDRow objectForKey:@"id"];
    NSString *onethumb = [getAlbumIDRow objectForKey:@"thumbnail"];
    NSString *onestatus = [getAlbumIDRow objectForKey:@"status"];
}

TableViewAppDelegate *dataCeter = (TableViewAppDelegate *)[[UIApplication sharedApplication] delegate];
dataCeter.dataSix = nil;
NSString *databaseURL = [NSString stringWithFormat:@"%@_process.jpg",newIDString];
dataCeter.dataSix = databaseURL;

[newIDString release];
[self presentModalViewController:captionView animated:NO];  
}

However, it saves it with a white border on a random side. Below is a example how it look (added a black background to make it more apparent).

I have narrowed it down to my resizing code:

// How to scale the image
- (UIImage *)scaleImage:(UIImage *) image: (float)maxWidth:(float) maxHeight {

CGImageRef imgRef = image.CGImage;

CGFloat width = CGImageGetWidth(imgRef);
CGFloat height = CGImageGetHeight(imgRef);

if (width <= maxWidth && height <= maxHeight)
{
    return image;
}

CGAffineTransform transform = CGAffineTransformIdentity;
CGRect bounds = CGRectMake(0, 0, width, height);

if (width > maxWidth || height > maxHeight)
{
    CGFloat ratio = width/height;
    if (ratio > 1)
    {
        bounds.size.width = maxWidth;
        bounds.size.height = bounds.size.width / ratio;
    }
    else
    {
        bounds.size.height = maxHeight;
        bounds.size.width = bounds.size.height * ratio;
    }
}

CGFloat scaleRatio = bounds.size.width / width;
UIGraphicsBeginImageContext(bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
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;
}

Please help, Thanks.

White line

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三人与歌 2024-11-15 11:05:18

UIImage imageWithCGImage:scale:orientation:只需传递在调整大小方法中计算的缩放值。

- (UIImage *)scaleImage:(UIImage *) image: (float)maxWidth:(float) maxHeight {

CGImageRef imgRef = image.CGImage;

CGFloat width = CGImageGetWidth(imgRef);
CGFloat height = CGImageGetHeight(imgRef);

if (width <= maxWidth && height <= maxHeight)
{
    return image;
}

CGRect bounds = CGRectMake(0, 0, width, height);

if (width > maxWidth || height > maxHeight)
{
CGFloat ratio = width/height;
if (ratio > 1)
{
    bounds.size.width = maxWidth;
    bounds.size.height = bounds.size.width / ratio;
}
else
{
    bounds.size.height = maxHeight;
    bounds.size.width = bounds.size.height * ratio;
}

CGFloat scaleRatio = bounds.size.width / width;
return [UIImage imageWithCGImage:imgRef scale:scaleRatio orientation:image. imageOrientation];

}

UIImage imageWithCGImage:scale:orientation: Just pass scale value calculated in your resize method.

- (UIImage *)scaleImage:(UIImage *) image: (float)maxWidth:(float) maxHeight {

CGImageRef imgRef = image.CGImage;

CGFloat width = CGImageGetWidth(imgRef);
CGFloat height = CGImageGetHeight(imgRef);

if (width <= maxWidth && height <= maxHeight)
{
    return image;
}

CGRect bounds = CGRectMake(0, 0, width, height);

if (width > maxWidth || height > maxHeight)
{
CGFloat ratio = width/height;
if (ratio > 1)
{
    bounds.size.width = maxWidth;
    bounds.size.height = bounds.size.width / ratio;
}
else
{
    bounds.size.height = maxHeight;
    bounds.size.width = bounds.size.height * ratio;
}

CGFloat scaleRatio = bounds.size.width / width;
return [UIImage imageWithCGImage:imgRef scale:scaleRatio orientation:image. imageOrientation];

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文