2013-05-29 133 views
0

我正在從imagePickerController中挑選圖像&使其調整爲80X80 &顯示在表中。exc_bad_access返回單元格後

下面

是回報單元后的cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] 

       initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     } 

    photobj= [appD.array1 objectAtIndex:indexPath.row]; 
    NSLog(@"photoobj=%@",photobj); 
    cell.imageView.frame=CGRectMake(0, 0, 80,80); 
    cell.imageView.tag=2000+indexPath.row; 

    UIImage *img1=[[[UIImage alloc]initWithData:photobj.imgdata]autorelease]; 
    NSLog(@"frame=%f",img1.size.width); 
    // img1=[UIImage imageWithData:photobj.imgdata]; 
    cell.imageView.image=img1; 
    NSLog(@"frame=%@",cell.imageView.image); 

    return cell;//after this , crash 
} 

代碼崩潰,如果我打電話以下圖像調整大小的方法

- (UIImage *)resizeImageToSize:(CGSize)targetSize withImage:(UIImage *)img 
{ 
    UIImage *sourceImage = img; 

    UIImage *newImage = [[UIImage alloc]init]; 

    CGSize imageSize = sourceImage.size; 
    CGFloat width = imageSize.width; 
    CGFloat height = imageSize.height; 

    CGFloat targetWidth = targetSize.width; 
    CGFloat targetHeight = targetSize.height; 

    CGFloat scaleFactor = 0.0; 
    CGFloat scaledWidth = targetWidth; 
    CGFloat scaledHeight = targetHeight; 

    CGPoint thumbnailPoint = CGPointMake(0.0,0.0); 

    if (CGSizeEqualToSize(imageSize, targetSize) == NO) { 

     CGFloat widthFactor = targetWidth/width; 
     CGFloat heightFactor = targetHeight/height; 

     if (widthFactor < heightFactor) 
      scaleFactor = widthFactor; 
     else 
      scaleFactor = heightFactor; 

     scaledWidth = width * scaleFactor; 
     scaledHeight = height * scaleFactor; 

     // make image center aligned 
     if (widthFactor < heightFactor) 
     { 
      thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; 
     } 
     else if (widthFactor > heightFactor) 
     { 
      thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5; 
     } 
    } 

    UIGraphicsBeginImageContext(targetSize); 
    CGRect thumbnailRect = CGRectZero; 
    thumbnailRect.origin = thumbnailPoint; 
    thumbnailRect.size.width = scaledWidth; 
    thumbnailRect.size.height = scaledHeight; 

    [sourceImage drawInRect:thumbnailRect]; 
    newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    if(newImage == nil) 
     NSLog(@"could not scale image"); 

    return newImage ; 
} 
下面

是代碼我在哪裏上述方法調用 的UIImage * imageEdited = [info objectForKey:UIImagePickerControllerOriginalImage];

self.img_imageView2.image=[self resizeImageToSize:CGSizeMake(80, 80) withImage:imageEdited ]; 
    photobj=[[PhotoObject alloc]init]; 

    photobj.imgdata=UIImageJPEGRepresentation(self.img_imageView2.image, 0.9); 
    [appD.array1 addObject:photobj]; 

如果我繞過這個imageresizing方法,那麼它工作正常,我沒有得到究竟是什麼問題?

+0

什麼是'imageEdited'? – borrrden

+0

它只是從imagepickercontroller拍攝的圖像 –

+0

哦,那麼在您的手機創建方法中,「img1」是什麼? – borrrden

回答

0

我會先創建一個UIImageView並將其添加到單元格中,而不是使用cell.imageView 我還將它設置在if語句中,並使用else語句從標記中調用它。否則,每次出現在視圖中時都會重新創建它。

嘗試

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

static NSString *CellIdentifier = @"Cell"; 
UIImageView *myImageView; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) 
{ 
    cell = [[UITableViewCell alloc] 

      initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

    cell.selectionStyle = UITableViewCellSelectionStyleNone; 

    myImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)] autorelease]; 
    myImageView.tag = 2000+indexPath.row; 
    [cell.contentView addSubview:myImageView]; 

    } 
    else 
    { 
     myImageView = (UIImageView*)[cell.contentView viewWithTag:2000+indexPath.row]; 
    } 

photobj= [appD.array1 objectAtIndex:indexPath.row]; 
NSLog(@"photoobj=%@",photobj); 


UIImage *img1=[[[UIImage alloc]initWithData:photobj.imgdata]autorelease]; 
NSLog(@"frame=%f",img1.size.width); 

myImageView.image = img1; 



    NSLog(@"frame=%@", imageView.image); 

return cell; 
} 

我使用的圖像大小調整下面的代碼。你只需要稍微調整一下你的尺寸參數並進行縮放

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo { 
image.image = img; 



[self _resizeImage]; 

[picker dismissModalViewControllerAnimated:YES]; 




} 



//function that is called by the image picker to resize the image for passing from one view to the next 
- (void) _resizeImage { 
NSInteger height; 
NSInteger width; 
height = 280; 
width = 280; 

CGImageRef imageRef = [image.image CGImage]; 
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef); 
CGColorSpaceRef colorSpaceInfo = CGColorSpaceCreateDeviceRGB(); 

if (alphaInfo == kCGImageAlphaNone) { 
    alphaInfo = kCGImageAlphaNoneSkipLast; 
} 

CGContextRef bitmap; 

if (image.image.imageOrientation == UIImageOrientationUp || image.image.imageOrientation == UIImageOrientationDown) { 
    bitmap = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, alphaInfo); 

} 
else { 
    bitmap = CGBitmapContextCreate(NULL, height, width, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, alphaInfo); 

} 

if (image.image.imageOrientation == UIImageOrientationLeft) { 
    CGContextRotateCTM(bitmap, radians(90)); 
    CGContextTranslateCTM(bitmap, 0, -height); 
} 
else if (image.image.imageOrientation == UIImageOrientationRight) { 
    CGContextRotateCTM(bitmap, radians(-90)); 
    CGContextTranslateCTM(bitmap, -width, 0); 
} 
else if (image.image.imageOrientation == UIImageOrientationUp) { 

} 
else if (image.image.imageOrientation == UIImageOrientationDown) { 
    CGContextTranslateCTM(bitmap, width, height); 
    CGContextRotateCTM(bitmap, radians(-180.)); 
} 


CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);  


CGImageRef ref = CGBitmapContextCreateImage(bitmap); 
UIImage *result = [UIImage imageWithCGImage:ref]; 

CGColorSpaceRelease(colorSpaceInfo); 
CGContextRelease(bitmap); 
CGImageRelease(ref); 
thisCopyImage = result; 
bgImage.image = thisCopyImage; 

} 
+0

沒有老兄不工作 –

+0

我已經添加了一些圖片調整大小代碼上面,因爲這似乎是什麼導致你的崩潰,你需要重寫它有點與您的輸入工作,但基礎知識是爲了從相機拍攝圖像並調整其大小。 –