2011-05-21 31 views
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; 
} 

請幫幫忙,謝謝。

White line

+0

使用imagePickerController:didFinishPickingMediaWithInfo:不是 - imagePickerController:didFinishPickingImage:editingInfo :.什麼是圖像的來源? – TheBlack 2011-05-21 03:44:39

+0

@TheBlack UIImagePickerControllerSourceTypeCamera或UIImagePickerControllerSourceTypePhotoLibrary。這張圖片的圖書館。 – iosfreak 2011-05-21 03:47:30

+0

另請參閱我的更新。謝謝! – iosfreak 2011-05-21 03:54:28

回答

1

的UIImage imageWithCGImage:規模:定位:只要通過你的大小調整方法計算比例值。

- (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]; 

}

+0

JK,我用'image.imageOrientation'取代了'image.orientation',它起作用。非常感謝! – iosfreak 2011-05-21 16:20:20